Using 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:
[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()
all(x in list for x in ('abc','def'))
Don't use list
as a variable name, it's a built-in type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.