I am trying to print concentric matrix. I have written the following code to print the lower half of the matrix
A=3
arr=[0]*(2*A-1)
matrix=[arr]*(2*A-1)
# print(matrix)
max=2*A-2
mid=max/2
for i in range(0,2*A-1):
for j in range(0,2*A-1):
if(i>=j):
matrix[i][j]=int(i-mid+1)
if(i<max-j):
matrix[i][j]=int(A-j)
print(matrix)
Output:
[[3, 0, 0, 0, 0], [3, 0, 0, 0, 0], [3, 0, 0, 0, 0], [3, 0, 0, 0, 0], [3, 0, 0, 0, 0]]
[[3, 2, 0, 0, 0], [3, 2, 0, 0, 0], [3, 2, 0, 0, 0], [3, 2, 0, 0, 0], [3, 2, 0, 0, 0]]
[[3, 2, 1, 0, 0], [3, 2, 1, 0, 0], [3, 2, 1, 0, 0], [3, 2, 1, 0, 0], [3, 2, 1, 0, 0]]
[[3, 2, 2, 2, 0], [3, 2, 2, 2, 0], [3, 2, 2, 2, 0], [3, 2, 2, 2, 0], [3, 2, 2, 2, 0]]
[[3, 3, 3, 3, 3], [3, 3, 3, 3, 3], [3, 3, 3, 3, 3], [3, 3, 3, 3, 3], [3, 3, 3, 3, 3]]
Why all the lists within the list matrix
are getting changed when only the ith list should be changed.