matrix = [[1,2,3],[4,5,6],[7,8,9]]
index = matrix
a,b=0,0
index[a][b] = matrix[a+1][b]
print(matrix)
If you output matrix
from the above code, even though you did not change the matrix
value
[[4, 2, 3], [4, 5, 6], [7, 8, 9]]
The matrix
value changes. Why are the prices changing and how do we solve them?
By index = matrix
, the index also has the value that matrix points to.
So index and matrix are the same. If you operate the index in that state, the value that matrix points to changes.
If you want to copy, you have to use the copy module to deepcopy.
import copy
index = copy.deepcopy(matrix)
© 2024 OneMinuteCode. All rights reserved.