为什么str.lower()修改修改字符串comp

发布于 2025-02-08 08:53:18 字数 389 浏览 2 评论 0原文

我不明白为什么要获得这些不同的结果,因此我有以下python代码剪切,并且已经发疯了。任何人都可以详细说明吗?

   test = "GTX1050Ti 4GB"

print(test)

if "gtx" and "560" and "ti" in test:
    print("GTX 560 Ti")
else:
    print("nope")
    
print(test.lower())

if "gtx" and "560" and "ti" in test.lower():
    print("GTX 560 Ti")
else:
    print("nope")``` 

Output:

GTX1050Ti 4GB
nope
gtx1050ti 4gb
GTX 560 Ti

I have the following python code snipped and have been getting insane as i don't understand why I'm getting these different results. Can anyone elaborate?

   test = "GTX1050Ti 4GB"

print(test)

if "gtx" and "560" and "ti" in test:
    print("GTX 560 Ti")
else:
    print("nope")
    
print(test.lower())

if "gtx" and "560" and "ti" in test.lower():
    print("GTX 560 Ti")
else:
    print("nope")``` 

Output:

GTX1050Ti 4GB
nope
gtx1050ti 4gb
GTX 560 Ti

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ可否回来 2025-02-15 08:53:18

您可以使用所有来帮助确保满足所有检查的条件

checks = ['gtx', '560', 'ti']
if all(check in test for check in checks):
    ....

if all(check in test.lower() for check in checks):
    ....

,然后如果需要更改所检查的内容,则只需要在一次位置进行操作即可。

You can use all to help ensure the condition is met for all checks

checks = ['gtx', '560', 'ti']
if all(check in test for check in checks):
    ....

if all(check in test.lower() for check in checks):
    ....

Then if you need to change what is checked, you only need to do it in once place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文