为什么在按引用传递的列表中进行更改不会在其他列表中进行更改

时间:2018-08-17 16:31:57

标签: python list pass-by-reference

我的老师告诉我purchase_list=my_list通过引用/地址传递。例如:

my_list = ['pen' , 'pencil' , 'cell phone' , 'axiom team' , 'panacloud' ]
purchase_list = my_list
my_list.append('x')
print(my_list)
print(purchase_list)

给出以下输出

['pen', 'pencil', 'cell phone', 'axiom team', 'panacloud', 'x']
['pen', 'pencil', 'cell phone', 'axiom team', 'panacloud', 'x']

那为什么当我执行以下操作时

my_list = ['pen' , 'pencil' , 'cell phone' , 'axiom team' , 'panacloud' ]
purchase_list = my_list
my_list=["Naufil"]
print(my_list)
print(purchase_list)

给我输出:

['Naufil']
['pen', 'pencil', 'cell phone', 'axiom team', 'panacloud']

也不会

['Naufil']
['Naufil']

0 个答案:

没有答案