嗨,有人可以阐明在Python中工作“in”运算符的机制。(a不在b)与(不在b中)之间的区别。 Python
现在我处理的例子如下:
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 ???
我现在在奇怪怎么可以如此。如果有人知道,请分享你的知识。 谢谢=)
'不在'和'不在'等于 – 2014-10-28 16:54:34
请注意,python styleguide说你应该使用'不在',即使他们这样做。 – 2014-10-28 16:58:21
你可以在['b',False]中很容易地将它改成'(不是'a'),这会给你你显然期望的答案(因为parens总是表示更高的优先级) – 2014-10-28 17:00:21