繁体   English   中英

检查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.

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