如何在python中测试两个列表是否相等

Att*_*s29 0 python if-statement equality list

当我认为它应该返回true时,以下脚本返回false.知道这里发生了什么吗?非常感谢,伙计们!

test=['Pop']
test1='Pop'

if (test==('POP' or 'Pop' or 'pop' or ['POP'] or ['Pop'] or ['pop'])):    
    print "yes"
else:
    print "no"
Run Code Online (Sandbox Code Playgroud)

目前,输出是'不'.

Win*_*ert 7

您不了解python如何处理该语句.Python不是自然语言.

if (test==(['Pop'] or 'Pop')):
Run Code Online (Sandbox Code Playgroud)

因为or它位于parens内部,所以它首先处理它.所以它看起来

['Pop'] or 'Pop'
Run Code Online (Sandbox Code Playgroud)

由于['Pop']被认为是True,因此python将整个语句简化为:

if (test==['Pop']):
Run Code Online (Sandbox Code Playgroud)

此时,它测试是否test等于['Pop']

你真正想要做的是:

(test == ['Pop']) or (test == 'Pop')
Run Code Online (Sandbox Code Playgroud)

这与完全不同

 test == (['Pop'] or 'Pop')
Run Code Online (Sandbox Code Playgroud)