+ 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?

10th Aug 2018, 5:04 AM
Hannah Wu
Hannah Wu - avatar
3 Réponses
+ 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)
10th Aug 2018, 3:50 PM
Hannah Wu
Hannah Wu - avatar
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.
10th Aug 2018, 8:44 AM
Detlef
Detlef - avatar
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
10th Aug 2018, 9:00 AM
Detlef
Detlef - avatar