I tried two ways of building a matrix in Python. After building, they seems to be the same. But if I do one same operation on them, they become different. I don't understand what is going on. Look at the following code.
m = [[0]*3]*3
n = [[0]*3 for i in range(3)]
print("m is:")
print(m)
print("n is:")
print(n)
print("m equals n? {}".format(m == n))
m[0][0] = 1
n[0][0] = 1
print('after operation')
print("m is:")
print(m)
print("n is:")
print(n)
print("m equals n? {}".format(m == n))
The output is:
m is:
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
n is:
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
m equals n? True
after operation
m is:
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]
n is:
[[1, 0, 0], [0, 0, 0], [0, 0, 0]]
m equals n? False