Feb-02-2023, 04:19 AM
I don't think this does what you think.
More importnatly, SiteVierge2d[0] is the same list as SiteVierge2d[1] and SiteVierge2d[2]... You can see that if you look at the object ID's. All 5 lists in SiteVierge2d are the same object.
I don't think this does what you think either:
https://docs.python.org/3/library/copy.html
SiteVierge2d=[[0]*(8)]*(5)To start with, the parenthesis have no effect here. The code runs the same with or without.
More importnatly, SiteVierge2d[0] is the same list as SiteVierge2d[1] and SiteVierge2d[2]... You can see that if you look at the object ID's. All 5 lists in SiteVierge2d are the same object.
SiteVierge2d=[[0]*(8)]*(5) print(*map(id, SiteVierge2d))
Output:1776206706304 1776206706304 1776206706304 1776206706304 1776206706304
It is the same as if you wrote this:zeros = [0] * 8 SiteVierge2d = [zeros] * 5I think you want to do something like this:
SiteVierge2d = [[0]*8 for _ in range(5)] print(*map(id, SiteVierge2d))
2218135596032 2218135268288 2218135123136 2218135124096 2218135599936Now each of the lists in SiteVierge2d are different lists because [0] * 8 was executed 5 times.
I don't think this does what you think either:
M=S[:]This does not make a deep copy. The lists in M are the same lists in S. If you set M[1][2] = 3 then S[1][2] == 3. If you want M to be a new 2D array with the same values as S, you need to do a deep copy.
https://docs.python.org/3/library/copy.html