如何使用 Python 'in' 运算符检查我的列表/元组是否包含整数 0、1、2?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35302215/
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
How to use Python 'in' operator to check my list/tuple contains each of the integers 0, 1, 2?
提问by G.Armstrong
How do I use the Python in
operator to check my list/tuple sltn
contains each of the integers 0, 1, and 2?
如何使用 Pythonin
运算符检查我的列表/元组是否sltn
包含整数 0、1 和 2?
I tried the following, why are they both wrong:
我尝试了以下方法,为什么它们都错了:
# Approach 1
if ("0","1","2") in sltn:
kwd1 = True
# Approach 2
if any(item in sltn for item in ("0", "1", "2")):
kwd1 = True
Update: why did I have to convert ("0", "1", "2")
into either the tuple (1, 2, 3)
? or the list [1, 2, 3]
?
更新:为什么我必须转换("0", "1", "2")
成元组(1, 2, 3)
?或名单[1, 2, 3]
?
采纳答案by Mohammed Aouf Zouag
if ("0","1","2") in sltn
You are trying to check whether the sltn
list contains the tuple ("0","1","2")
, which it does not. (It contains 3 integers)
您正在尝试检查sltn
列表是否包含 tuple ("0","1","2")
,而它不包含。(它包含 3 个整数)
But you can get it done using #all():
但是您可以使用#all()完成它:
sltn = [1, 2, 3] # list
tab = ("1", "2", "3") # tuple
print(all(int(el) in sltn for el in tab)) # True
回答by L3viathan
To check whether your sequence contains allof the elements you want to check, you can use a generator comprehension in a call to all
:
要检查您的序列是否包含您要检查的所有元素,您可以在调用中使用生成器推导式all
:
if all(item in sltn for item in ("0", "1", "2")):
...
If you're fine with either of them being inside the list, you can use any
instead:
如果您对列表中的任何一个都满意,则可以any
改用:
if any(item in sltn for item in ("0", "1", "2")):
...
回答by timgeb
Using the in
keyword is a shorthand for calling an object's __contains__
method.
使用in
关键字是调用对象__contains__
方法的简写。
>>> a = [1, 2, 3]
>>> 2 in a
True
>>> a.__contains__(2)
True
Thus, ("0","1","2") in [0, 1, 2]
asks whether the tuple ("0", "1", "2")
is containedin the list [0, 1, 2]
. The answer to this question if False
. To be True
, you would have to have a list like this:
因此,("0","1","2") in [0, 1, 2]
询问元组("0", "1", "2")
是否包含在列表中[0, 1, 2]
。这个问题的答案如果False
。要成为True
,你必须有一个这样的列表:
>>> a = [1, 2, 3, ("0","1","2")]
>>> ("0","1","2") in a
True
Please also note that the elements of your tuple are strings. You probably want to check whether any or all of the elements in your tuple - after converting these elements to integers- are contained in your list.
另请注意,元组的元素是字符串。您可能想要检查元组中的任何或所有元素 -在将这些元素转换为整数之后- 是否包含在您的列表中。
To check whether all elements of the tuple (as integers) are contained in the list, use
要检查元组的所有元素(作为整数)是否包含在列表中,请使用
>>> sltn = [1, 2, 3]
>>> t = ("0", "2", "3")
>>> set(map(int, t)).issubset(sltn)
False
To check whether any element of the tuple (as integer) is contained in the list, you can use
要检查列表中是否包含元组的任何元素(作为整数),您可以使用
>>> sltn_set = set(sltn)
>>> any(int(x) in sltn_set for x in t)
True
and make use of the lazy evaluation any
performs.
并利用惰性求值any
执行。
Of course, if your tuple contains strings for no particular reason, just use(1, 2, 3)
and omit the conversion to int.
当然,如果您的元组没有特殊原因包含字符串,只需使用(1, 2, 3)
并省略到 int 的转换。
回答by Pouria
In case you don't want waste time and iterate through all the data in your list, as widely suggested around here, you can do as follows:
如果您不想浪费时间并遍历列表中的所有数据,正如此处广泛建议的那样,您可以执行以下操作:
a = ['1', '2', '3']
b = ['4', '3', '5']
test = set(a) & set(b)
if test:
print('Found it. Here it is: ', test)
Of course, you can do if set(a) & set(b)
. I didn't do that for demonstration purposes. Note that you shouldn't replace &
with and
. They are two substantially different operators.
当然,你可以这样做if set(a) & set(b)
。我这样做不是为了演示目的。请注意,您不应替换&
为and
. 他们是两个完全不同的运营商。
The above code displays:
上面的代码显示:
Found it. Here it is: {'3'}