简体   繁体   English

检查python列表的内容

[英]Checking the contents of a python list

Using python 2.7.4 使用python 2.7.4

Lets say I have a list 可以说我有一个清单

list = ['abc', 'def']

I want to find if it contains certain things. 我想查找它是否包含某些东西。 So I try: 所以我尝试:

 [IN:] 'abc' in list
[OUT:] True
 [IN:] 'def' in list
[OUT:] True
 [IN:] 'abc' and 'def' in list
[OUT:] True

But when I list.pop(0) and repeat that last test: 但是当我列出.pop(0)并重复上一次测试时:

 [IN:] 'abc' and 'def in list
[OUT:] True

Even though: 即使:

list = ['def']

Anybody know why? 有人知道为什么吗?

That's because: 那是因为:

abc' and 'def' in list

is equivalent to: 等效于:

('abc') and ('def' in list) #Non-empty string is always True

Use 'abc' in list and 'def' in list or for multiple items you can also use all() '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'))

Don't use list as a variable name, it's a built-in type. 不要将list用作变量名,它是内置类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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