+ 2
Can anyone explain why the result for these two code is different?
Code as below: >>> L=[0,1,2,3,4] >>> print ([x*100+y*10+z for x in L for y in L for z in L if x==z & z!=0]) [101, 111, 121, 131, 141, 202, 212, 222, 232, 242, 303, 313, 323, 333, 343, 404, 414, 424, 434, 444] >>> print ([x*100+y*10+z for x in L for y in L for z in L if x==z & x!=0]) [101, 103, 111, 113, 121, 123, 131, 133, 141, 143, 202, 203, 212, 213, 222, 223, 232, 233, 242, 243, 303, 313, 323, 333, 343, 404, 414, 424, 434, 444] why the output of second one, x is not always same as z?
3 odpowiedzi
+ 2
i got it.
>>> for x in L:
for y in L:
for z in L:
if x==z & x!=0:
print (x,y,z)
print (x*100+y*10+z)
the result is different with:
>>> for x in L:
for y in L:
for z in L:
if x==z & z!=0:
print (x,y,z)
print (x*100+y*10+z)
0
Hm, i just try the code. I thought it was a buffering problem. But if i copy the lines they work as expected. Twice the first result.
0
hey stop! Found a difference. Sorry i miss the 'z!=0' versus 'x!=0' and thats the point.
In the second version must 'x equal z AND not 0' thats why it cant be always the same as z