s = set(['Adam', 'Lisa', 'Paul'])L = ['Adam', 'Lisa', 'Bart', 'Paul']print True == True #得到Trueprint 'Adam' in s #得到Trueprint 'Adam' in s == True #得到False如题
2 回答
已采纳
秋名山车神
TA贡献54条经验 获得超303个赞
说实话,这个问题我之前也不知道是为什么,于是抱着求知的心态,问了国外的大神,Михаил Никонов的回答原文如下:
It's because of comparison operator chaining, I think. In Python, expression (x < y < z) is, unlike in C, equivalent to (x < y and y < z), and that can be used for any sequence of comparisons (including membership tests). So basically, without parentheses, your last expression is equivalent to ('a' in 'abc' and 'abc' == True), or so it seems.
翻译过来差不多就是,print('a' in 'abc' == True) 这种表达式,在Python里面不像C语言那样,它必须要增加一个括号:print(('a' in 'abc') == True) 这样才对,如果不增加括号,就相当于print('a' in 'abc' and 'abc' == True) 结果当然是 False。
添加回答
举报
0/150
提交
取消