简体   繁体   English

如果在元组中为string1或string2

[英]If string1 or string2 in tuple

So I stumbled upon a problem (solved it eventually via experimentation) with checking for the existence of strings inside a tuple. 所以我偶然发现了一个问题(最终通过实验解决了),检查了元组中是否存在字符串。

if 'String1' or 'String2' in tuple:

evaluated True even if neither were in tuple. 即使两个都不在元组中,它也评估为True

if 'String1' in tuple or 'String2' in tuple:

gives me the desired result. 给我想要的结果。

What did the first variant actually evaluate? 第一个变体实际评估了什么?

Edit: @Cricket_007 ... the question you pointed to in your dupe report didn't cover the in , therefore I believe this not to be a dupe. 编辑:@ Cricket_007 ...您在欺骗报告中指出的问题没有涵盖in ,因此我相信这不是欺骗。

The difference is the in container checking, If you break this statement: if 'String1' or 'String2' in tuple: 区别在于in容器中检查,如果您破坏了以下语句: if 'String1' or 'String2' in tuple:

  • 'String1' returns True because testing str() returns True as long as the string is not empty 'String1'返回True,因为只要字符串不为空,测试str()返回True

  • 'String2' in tuple returns True only if the string is contained in the tuple 'String2' in tuple仅在字符串包含在元组中时才返回True

The first condition always returns True . 第一个条件始终返回True

The second line tests both variables for in containment first, and then performing or between the results 第二行测试两个变量为in容纳第一,然后执行or结果之间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM