![](/img/trans.png)
[英]Finding the divisors of every content of a list by checking the contents of another list in Python
[英]Checking the contents of a python list
使用python 2.7.4
可以说我有一个清单
list = ['abc', 'def']
我想查找它是否包含某些东西。 所以我尝试:
[IN:] 'abc' in list
[OUT:] True
[IN:] 'def' in list
[OUT:] True
[IN:] 'abc' and 'def' in list
[OUT:] True
但是当我列出.pop(0)并重复上一次测试时:
[IN:] 'abc' and 'def in list
[OUT:] True
即使:
list = ['def']
有人知道为什么吗?
那是因为:
abc' and 'def' in list
等效于:
('abc') and ('def' in list) #Non-empty string is always True
'abc' in list and 'def' in list
使用'abc' in list and 'def' in list
使用'abc' in list and 'def' in list
或者对于多个项目,还可以使用all()
all(x in list for x in ('abc','def'))
不要将list
用作变量名,它是内置类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.