Python: Something went wrong somewhere in the list comprehension?

>>> [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 

So litheor(2) is True and 2 in sieve(100) is True , so the if clause in the list comprehension is False . But why is 2 still at the bottom of the list comprehension?

+6
source share
1 answer

Well, at first it sounds crazy, but:

 >>> True != 2 in [2,3,5] True >>> (True != 2) in [2,3,5] False >>> True != (2 in [2,3,5]) False 

When you realize that this is not a simple priority problem, viewing the AST is the only option left:

 >>> 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())]) )])" 

And here's a little hint:

 >>> 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)]) )])' 

So, it turns out that True != 2 in [2,3,5] interpreted as 1 < 2 <= 3 . And your expression

 litheor(l) != l in sieve(100) 

means

 litheor(l) != l and l in sieve(100) 

which is True .

+11
source

Source: https://habr.com/ru/post/946729/


All Articles