转自: https://blog.csdn.net/dengwubin450/article/details/57980514
1.浅拷贝(copy),深拷贝
1.1 浅拷贝
# coding:utf-8# python3 -- list列表操作(拷贝copy)# 注意文件命名方式:不能 与关键字copy等发生冲突# 浅拷贝,只拷贝第一层,2层以上 都是拷贝元素的地址list_names = ["he", "li", ["liu", "li"], "fu", "chen"]list_names2 = list_names.copy()list_names[3] = "平"print(list_names)print(list_names2)# 只是name,指向了list_names这个列表存储地址name = list_namesprint(name)# 多维列表:,所以2层以后的元素,会跟着原来的列表改变list_names[2][0] = "高"print(list_names)print(list_names2)
2.深拷贝
# coding:utf-8# python3 -- list列表操作(深拷贝copy)import copy# 深拷贝:拷贝的内容 不会随原列表list_names内容的更改而更改list_names = ["he", "li", ["liu", "li"], "fu", "chen"]list_names2 = copy.deepcopy(list_names)list_names[3] = "平"print(list_names)print(list_names2)# 多维列表list_names[2][0] = "高"print(list_names)print(list_names2)