在使用列表时使用'='等号是否可以?即使在myList被更改后,下面的代码也会导致myOtherList的一致行为。是否有使用.append()而不是'='直等号的隐藏好处?Python:列出等于列表或对于列表中的每个列表附加
myList=[1, 2, 3, 4, 5]
myOtherList=myList
myThirdList=[]
for each in myList:
myThirdList.append(each)
print myOtherList==myThirdList # True
myList=[]
print myOtherList==myThirdList # True
名称“mylist”被反弹,指向一个新的空列表。原始列表尚未更改。 myOtherList仍指向原始列表。所以是的,这是好的,如果这是你想要它做的。 – TessellatingHeckler