python3 list append list

when you append a list to a list, something needs to be noted:

1 a  = 1
2 b =  []
3 for i  in range(4):
4     a = a + b
5     b.append(a)

the result is right, but when the a is a list like a=[1,1,1,1]:

1 a =  [-1,-1,-1,-1]
2 b = []
3 for i in range(4):
4     a[i] = 1
5     b.append(a)

the result will be wrong with what we need.

Due to the append use the object, and the result will be the object's result.

原文地址:https://www.cnblogs.com/WaterGood/p/12488624.html