浅拷贝和深拷贝:
浅拷贝 shallow copy
浅拷贝是指在对象复制过程中,只复制一层变量,不会复制深层
变量绑定的对象的复制过程
例子:
L = [3.1,3.2]
L1 = [1,2,L]
l2 = L1.copy() #浅拷贝
L[0] = 3.14 # l2[2][0] = 3.14
print(L1) # L1 = [1,2,[3.14,3.2]]
print(L2) # L2 = [1,2,[3.14,3.2]]
print(L) #[3.14,3.2]
深拷贝 deep copy:
import copy #导入复制模块
L = [3.1,3.2]
L1 = [1,2,L]
L2 = copy.deepcopy(L1) #深拷贝
L2[2][0] = 3.14
print(L1) # L1 = [1,2,[3.1,3.2]]
print(L2) # L2 = [1,2,[3.14,3.2]]
print(L) #[3.1,3.2]
网友评论