I have the following code:
def matrixReshape(mat, r, c):
if len(mat)*len(mat[0]) != r*c:
return mat
nums = []
for row in range(len(mat)):
for col in range(len(mat[0])):
nums.append(mat[row][col])
res = [[0]*c]*r
for x in range(r):
for y in range(c):
res[x][y] = nums.pop(0)
return res
mat = [[1,2],[3,4]]
print(matrixReshape(mat, 4, 1))
Which results in the output:
[[4], [4], [4], [4]]
But I want it to output:
[[1], [2], [3], [4]]
I'm confused why the entire column gets updated. When I print x
and y
in the 2nd for
loops I get: 0,0 1,0 2,0 3,0
as expected.