复制列表的方法# coding = utf-8
import
copy
list
=
[
1
,
2
,
3
]
# 普通赋值(引用)
list_a
=
list
# 切片赋值(引用)
list_b
=
list
[:]
# 浅拷贝
list_c
=
list
.copy()
list_d
=
copy.copy(
list
)
# 需要导入copy包
# 深拷贝
list_e
=
copy.deepcopy(
list
)
# 需要导入copy包
names = [1,2,3,4,5,[1,2]]
浅copy:
n1=names.copy()
names[2] = 8 names = [1,2,8,4,5,[1,2]] n1 = [1,2,3,4,5,[1,2]]
names[-1][0] = 8 names = [1,2,8,4,5,[8,2]] n1=[1,2,3,4,5,[8,2]]
深copy:
n2=copy.deepcopy(names)
names[-1][0] = 6 names = [1,2,3,4,5,[6,2]] n2 = [1,2,3,4,5,[1,2]]