浅拷贝
lst = [[1, 2], 3, 4] from_lst = lst.copy() # 浅拷贝 print(from_lst) # [[1, 2], 3, 4] from_lst[1] = 3333 # 修改这个元素的值,不影响lst print(from_lst) # [[1, 2], 3333, 4] print(lst) # [[1, 2], 3, 4] from_lst[0][1] = 2222 print(from_lst) # [[1, 2222], 3333, 4] print(lst) # [[1, 2222], 3, 4]
深拷贝
import copy lst = [[1, 2], 3, 4] from_lst = copy.copy(lst) # 浅拷贝 print(from_lst) # [[1, 2], 3, 4] deep_lst = copy.deepcopy(lst) # 深拷贝 print(deep_lst) # [[1, 2], 3, 4] deep_lst[0][1] = 1111 # 不影响原来的lst print(deep_lst) # [[1, 1111], 3, 4] print(lst) # [[1, 2], 3, 4]