深拷贝
- 复制后的内容改变,原来的列表不受影响
- copy.deepcopy()
浅拷贝
- 复制后的内容改变,原来的列表也受影响
- 只会复制顶层对象,不影响深层的可变数据类型
- copy.copy()
import copy
num = [1,2,3,[1,2]]
num_new = copy.copy(num)
print(num_new)
num_new.append(5)
print(num)
print(num_new)
num_new[3][0] = 3
print(num)
print(num_new)
import copy
num = [1,2,3,[1,2]]
num_new = copy.deepcopy(num)
print(num_new)
num_new.append(5)
print(num)
print(num_new)
num_new[3][0] = :3
print(num)
print(num_new)
输出:
[1, 2, 3, [1, 2]]
[1, 2, 3, [1, 2]]
[1, 2, 3, [1, 2], 5]
[1, 2, 3, [3, 2]]
[1, 2, 3, [3, 2], 5] #改变列表中的列表,原列表跟着改变
[1, 2, 3, [1, 2]]
[1, 2, 3, [1, 2]]
[1, 2, 3, [1, 2], 5]
[1, 2, 3, [1, 2]]
[1, 2, 3, [3, 2], 5] #改变列表中的列表,原列表不改变
标签:列表,num,深浅,print,new,拷贝,copy
From: https://www.cnblogs.com/zenopan101861/p/18120517