你好,能否有人解释一下 Python 中 "in" 运算符的机制。
我现在正在处理以下示例:
print ('a' not in ['a', 'b']) # outputs False
print (not 'a' in ['a', 'b']) # outputs False -- how ???
print ('c' not in ['a', 'b']) # outputs True
print (not 'c' in ['a', 'b']) # outputs True
print (not 'a') # outputs False
# ok is so then...
print (not 'a' in ['b', False]) # outputs True --- why ???
我现在很想知道为什么会这样。如果有人知道,请分享你的知识。 谢谢 =)
a not in
和not a in
是等价的。 - Tima not in
,即使它们的作用相同。 - Tim(not 'a') in ['b', False]
,这将给出你期望的答案(因为括号始终表示更高的优先级)。 - Joran Beasleynot a in b
的语句转换为a not in b
。因此,这是使用not in
的另一个原因,而且在我看来更易读。 - Ashwini Chaudhary