深浅拷贝
import copy lst1 = ["金毛狮王", "紫衫龙王", "青翼蝠王", "白眉鹰王",["张无忌","赵敏","周芷若"]] # lst2 = lst1[:] # 浅拷贝 # lst2 = lst1.copy() # 浅拷贝 lst2 = copy.deepcopy(lst1) # lst1[4].append("小昭") print(lst1) print(lst2) print(id(lst1[4])) print(id(lst2[4]))
1. 赋值. 没有创建新对象. 公用同一个对象
2. 浅拷贝. 拷贝第一层内容. [:]或copy()
3. 深拷贝. 拷贝所有内容. 包括内部的所有.