Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 in the output of the list comprehension?

like image 649
Sylvester V Lowell Avatar asked Jun 06 '13 21:06

Sylvester V Lowell


1 Answers

Ok, 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 realise that this is not a simple precedence issue, looking at the AST is the only remaining option:

>>> 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 is 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, True != 2 in [2,3,5] is interpreted similar to 1 < 2 <= 3. And your expression

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

means

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

which is True.

like image 178
kirelagin Avatar answered Nov 07 '22 18:11

kirelagin