Jan-21-2021, 10:00 AM
The problem is how you create it on this line
torus = [[0] * MAXHORZ] * MAXVERTlists are mutable and you populate with same object, thus when you update one, it is reflected elsewhere
rows = 3 cols = 4 spam = [[0] * cols] * rows print(spam) print([id(row) for row in spam]) spam[0][0] = 1 print(spam) print('-----------------------') eggs = [[0] * cols for _ in range(rows)] print(eggs) print([id(row) for row in eggs]) eggs[0][0] = 1 print(eggs)
Output:[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[12470912, 12470912, 12470912]
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
-----------------------
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[13534272, 13534312, 13534152]
[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs