使用“in”运算符(Python)进行多值检查
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2308944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple value checks using 'in' operator (Python)
提问by eozzy
if 'string1' in line: ...
... works as expected but what if I need to check multiple strings like so:
...按预期工作,但如果我需要像这样检查多个字符串怎么办:
if 'string1' or 'string2' or 'string3' in line: ...
... doesn't seem to work.
......似乎不起作用。
回答by Ignacio Vazquez-Abrams
if any(s in line for s in ('string1', 'string2', ...)):
回答by John La Rooy
If you read the expression like this
如果你读这样的表达
if ('string1') or ('string2') or ('string3' in line):
The problem becomes obvious. What will happen is that 'string1' evaluates to True so the rest of the expression is shortcircuited.
问题就很明显了。将会发生的是 'string1' 的计算结果为 True,因此表达式的其余部分被短路。
The long hand way to write it is this
写它的长手方式是这样的
if 'string1' in line or 'string2' in line or 'string3' in line:
Which is a bit repetitive, so in this case it's better to use any()
like in Ignacio's answer
这有点重复,所以在这种情况下最好使用any()
Ignacio 的回答
回答by Eric Bannatyne
if 'string1' in line or 'string2' in line or 'string3' in line:
Would that be fine for what you need to do?
这对你需要做的事情没问题吗?
回答by Mike Graham
or
does not behave that way. 'string1' or 'string2' or 'string3' in line
is equivalent to ('string1') or ('string2') or ('string3' in line)
, which will always return true (actually, 'string1'
).
or
不会那样做。'string1' or 'string2' or 'string3' in line
等价于('string1') or ('string2') or ('string3' in line)
,它总是返回真(实际上,'string1'
)。
To get the behavior you want, you can say if any(s in line for s in ('string1', 'string2', 'string3')):
.
要获得您想要的行为,您可以说if any(s in line for s in ('string1', 'string2', 'string3')):
.
回答by Kshitij Dhyani
You have this confusion Because you don't understand how the logical operator works with respect to string.
Python considers empty strings as False and Non empty Strings as True.
Proper functioning is :
a and b returns b if a is True, else returns a.
a or b returns a if a is True, else returns b.
Therefore every time you put in a non empty string in place of string1 the condition will return True and proceed , which will result in an undesired Behavior . Hope it Helps :).
您有这种困惑,因为您不了解逻辑运算符如何处理字符串。
Python 将空字符串视为 False,将非空字符串视为 True。
正确的运作是:
如果 a 为真,则 a 和 b 返回 b,否则返回 a。
如果 a 为真,则 a 或 b 返回 a,否则返回 b。
因此,每次在 string1 的位置放置一个非空字符串时,条件都会返回 True 并继续,这将导致不希望的 Behavior 。希望能帮助到你 :)。
回答by Daniel Moon
also as for "or", you can make it for "and". and these are the functions for even more readablity:
同样对于“或”,您可以将其设为“和”。这些是更具可读性的功能:
returns true if any of the arguments are in "inside_of" variable:
如果任何参数在“inside_of”变量中,则返回 true:
def any_in(inside_of, arguments):
return any(argument in inside_of for argument in arguments)
returns true if all of the arguments are in "inside_of" variable:
如果所有参数都在“inside_of”变量中,则返回 true:
same, but just replace "any" for "all"
相同,但只需将“any”替换为“all”
回答by Ho0ony
Using map
and lambda
使用map
和lambda
a = ["a", "b", "c"]
b = ["a", "d", "e"]
c = ["1", "2", "3"]
# any element in `a` is a element of `b` ?
any(map(lambda x:x in b, a))
>>> True
# any element in `a` is a element of `c` ?
any(map(lambda x:x in c, a)) # any element in `a` is a element of `c` ?
>>> False
and high-order function
和高阶函数
has_any = lambda b: lambda a: any(map(lambda x:x in b, a))
# using ...
f1 = has_any( [1,2,3,] )
f1( [3,4,5,] )
>>> True
f1( [6,7,8,] )
>>> False