深浅拷贝

# 浅拷贝
# 浅拷贝只会拷贝第一层的数据,修改拷贝数据的第一层不会变更原数据,超过第一层的数据会把原数据也修改

shallow_list = [[1, 2], 3, 4]

shallow_list1 = shallow_list.copy()
shallow_list1[1] = "123"
print(shallow_list1)
print(shallow_list)
shallow_list1[0][1] = "568"
print(shallow_list1)
print(shallow_list)


# 深拷贝
# 深拷贝会把整个列表中的数据都拷贝,修改拷贝后的数据不会影响原数据

deep_list = [[1, 2], 3, 4]
deep_list1 = copy.deepcopy(deep_list)
deep_list1[1] = "321"
print(deep_list1)
print(deep_list)
deep_list1[0][1] = "999"
print(deep_list1)
print(deep_list)

 

posted @ 2024-08-21 16:46  GDquicksand  阅读(3)  评论(0编辑  收藏  举报