浅拷贝
lst = [[1, 2], 3, 4]from_lst = lst.copy() # 浅拷贝print(from_lst) # [[1, 2], 3, 4]from_lst[1] = 3333 # 修改这个元素的值,不影响lstprint(from_lst) # [[1, 2], 3333, 4]print(lst) # [[1, 2], 3, 4]from_lst[0][1] = 2222print(from_lst) # [[1, 2222], 3333, 4]print(lst) # [[1, 2222], 3, 4]
深拷贝
import copylst = [[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 # 不影响原来的lstprint(deep_lst) # [[1, 1111], 3, 4]print(lst) # [[1, 2], 3, 4]