0
可能重複:
What is the difference between LIST.append(1) and LIST = LIST + [1] (Python)差異
我對參數是如何傳遞給函數的疑問和他們的可變性,特別是在案件列表。
考慮以下...
def add_list(p):
p = p + [1]
def append_list(p):
p.append(1)
p = [1, 2, 3]
add_list(p)
print p
append_list(p)
print p
輸出我得到的是...
[1, 2, 3]
[1, 2, 3, 1]
爲什麼當我追加到它的功能原始列表的變化,但不變如果我使用operator +?
追加變異。添加副本。 –
Short Answer:'append()'改變列表** in-place **和'+'返回一個新列表,它根本不影響原始列表。 –