【发布时间】:2020-02-18 17:46:05
【问题描述】:
我最近被这个弄糊涂了
if 2 in ([1,2] or [3,4]) : print(True)
else: print(False)
#prints True
-
or是一个布尔运算符,那么它如何应用于列表呢? - 为什么它和
if 2 in [1,2] or [3,4]一样工作?
【问题讨论】:
-
那个条件实际上应该是
if any(2 in sub for sub in [[1,2], [3,4]]): -
回答您的其他问题:stackoverflow.com/questions/39983695/… 非空列表被认为是“真实的”,因此
[1,2] or [3,4]的计算结果为True
标签: python syntax operator-precedence