Reputation: 1
solving a problem in leetcode, I needed to create a matrix and take data from another matrix, for this I wrote res[0][0]=s[0][0], but for some reason Python assigned all res[0-2][0] values to s[0][0]
s= [[-2,-3,3],[-5,-10,1],[10,30,-5]]
res=[[0]*len(s)]*len(s[0])
res[0][0]=s[0][0]
print(res)
output: [[-2, 0, 0], [-2, 0, 0], [-2, 0, 0]] but i want [[-2, 0, 0], [0, 0, 0], [0, 0, 0]]
s= [[-2,-3,3],[-5,-10,1],[10,30,-5]]
res=[[0]*len(s)]*len(s[0])
res[1][0]=s[0][0]
print(res)
output: [[-2, 0, 0], [-2, 0, 0], [-2, 0, 0]]
Upvotes: 0
Views: 26