D
davidalvi
Consider these two lists comprehensions:
L1=[[1 for j in range(3)] for i in range(3)]
L2=[[1]*3]*3
print L1
print L2
print L1==L2
The result is:
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
True
So far, everything is OK, but let us now modify the lists' contents in
the following way:
for i in range(3):
for j in range(3):
if j<i:
L1[j]=0
L2[j]=0
print L1
print L2
print L1==L2
The output is now:
[[1, 1, 1], [0, 1, 1], [0, 0, 1]]
[[0, 0, 1], [0, 0, 1], [0, 0, 1]]
False
It seems a misbehaviour in Python, or there is something I do not
understand in the syntax ????
L1=[[1 for j in range(3)] for i in range(3)]
L2=[[1]*3]*3
print L1
print L2
print L1==L2
The result is:
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
True
So far, everything is OK, but let us now modify the lists' contents in
the following way:
for i in range(3):
for j in range(3):
if j<i:
L1[j]=0
L2[j]=0
print L1
print L2
print L1==L2
The output is now:
[[1, 1, 1], [0, 1, 1], [0, 0, 1]]
[[0, 0, 1], [0, 0, 1], [0, 0, 1]]
False
It seems a misbehaviour in Python, or there is something I do not
understand in the syntax ????