给定一个列表xs
和一个值item
,如何检查是否xs
包含item
(即,如果任何元素xs
等于item
)?有没有类似的东西xs.contains(item)
?
For performance considerations, see Fastest way to check if a value exists in a list https://stackoverflow.com/questions/7571635.
Use:
if my_item in some_list:
...
另外,逆运算:
if my_item not in some_list:
...
它适用于列表、元组、集合和字典(检查键)。
Note这在列表和元组中是 O(n) 操作,但在集合和字典中是 O(1) 操作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)