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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM