Python:列表中的某个地方出了问题?

Python:列表中的某个地方出了问题?,python,list-comprehension,primes,Python,List Comprehension,Primes,因此,litheor(2)为True,筛(100)中的2为True,因此列表理解中的if子句为False。但是为什么列表的输出中仍然有2 好吧,一开始听起来很疯狂,但是: >>> [l for l in range(2,100) if litheor(l)!=l in sieve(100)] [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 9

因此,
litheor(2)
True
,筛(100)中的
2为
True
,因此列表理解中的
if
子句为
False
。但是为什么列表的输出中仍然有
2

好吧,一开始听起来很疯狂,但是:

>>> [l for l in range(2,100) if litheor(l)!=l in sieve(100)]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
>>> 2 in sieve(100)
True
>>> litheor(2)
True
当您意识到这不是一个简单的优先级问题时,查看AST是唯一剩下的选项:

>>> True != 2 in [2,3,5]
True
>>> (True != 2) in [2,3,5]
False
>>> True != (2 in [2,3,5])
False
这里有一点提示:

>>> ast.dump(ast.parse("True != 2 in [2,3,5]"))
"Module(body=[Expr(value=
Compare(left=Name(id='True', ctx=Load()), ops=[NotEq(), In()], comparators=[Num(n=2), List(elts=[Num(n=2), Num(n=3), Num(n=5)], ctx=Load())])
)])"
意味着


这是
正确的

谢谢。然而,1不在筛(100)中。还有什么可能出错的吗?@SylvesterVLowell,可能是以下情况之一:1)
sieve(100)
不仅包含整数,还包含布尔值
True
;2)
litheor(2)
返回
2
而不是布尔和
sieve(100)
包含
0
。我看不出还有其他选择。你能给我们看看你对
litheor
的定义吗?@SylvesterVLowell哦,等等……你说得对……那太疯狂了…
>>> ast.dump(ast.parse("1 < 2 <= 3"))
'Module(body=[Expr(value=
Compare(left=Num(n=1), ops=[Lt(), LtE()], comparators=[Num(n=2), Num(n=3)])
)])'
litheor(l) != l in sieve(100)
litheor(l) != l and l in sieve(100)