Nov-08-2019, 10:20 AM
Thanks a lot
Okay, so we can say that a1, a2, a3 and b1 have 1 common point: they all build a list of lists.
For a1, a2 & a3, the items that make up their list of lists all point to the original list.
For exemple:
For exemple:
For a3 the sub-list will be assigned in literal form. In a list, some references from this literal list will be simply copied n times (by multiplication) for a result similar to a1 & a2.
For b1 the sub-list will be assigned in literal form and re-evaluated for each copy (by iteration) in a list for n independent copy.
So for a2 =[tt for _ in range(4)], tt is not re-evaluated because it refers to the list assigned to it, unlike b1 = [[0,1] for _ in range(4)].
For a1 & a3 it is a simple copy, from a list assigned for a1 and from a list expressed literally for a3.
Is it correct?
Okay, so we can say that a1, a2, a3 and b1 have 1 common point: they all build a list of lists.
For a1, a2 & a3, the items that make up their list of lists all point to the original list.
For exemple:
tt = [0,1] a1 = [tt]*4 a2 = [tt for _ in range(4)] a1[0][1] = 6 #Or a2[0][1] = 6 print(tt)
Output:[0, 6]
The elements that make up the b1 list of lists all point to themselves in the original list.For exemple:
b1 = [[0,1] for _ in range(4)] b1[0][1] = 6 cc = b1[0] print(cc) print(b1)
Output:[0, 6]
[[0, 6], [0, 1], [0, 1], [0, 1]]
cc[1] = 2 print(cc) print(b1)
Output:[0, 2]
[[0, 2], [0, 1], [0, 1], [0, 1]]
tt is assigned by a list. So for a1 & a2 we copy n times (by multilication and iteration) into a list some references of the list tt.For a3 the sub-list will be assigned in literal form. In a list, some references from this literal list will be simply copied n times (by multiplication) for a result similar to a1 & a2.
For b1 the sub-list will be assigned in literal form and re-evaluated for each copy (by iteration) in a list for n independent copy.
So for a2 =[tt for _ in range(4)], tt is not re-evaluated because it refers to the list assigned to it, unlike b1 = [[0,1] for _ in range(4)].
For a1 & a3 it is a simple copy, from a list assigned for a1 and from a list expressed literally for a3.
Is it correct?