python深浅copy
# 在列表中有嵌套列表的情况下才会去讨论深浅复制
# 深复制 from copy import deepcopy a = [11, 22, 33] li = [1, 2, 3, a] # 浅copy li_cp = li.copy() # 深copy li_dpcp = deepcopy(li) print(id(li[3])) print(id(li_cp[3])) print(id(li_dpcp[3])) print("li", li) print("浅复制", li_cp) print("深复制", li_dpcp) print('------------------------') a.append(789) print("li", li) print("浅复制", li_cp) print("深复制", li_dpcp)
答案:
1487939967240 1487939967240 1487940098760 li [1, 2, 3, [11, 22, 33]] 浅复制 [1, 2, 3, [11, 22, 33]] 深复制 [1, 2, 3, [11, 22, 33]] ------------------------ li [1, 2, 3, [11, 22, 33, 789]] 浅复制 [1, 2, 3, [11, 22, 33, 789]] 深复制 [1, 2, 3, [11, 22, 33]]