深拷贝 deep copy 和 浅拷贝 shallow copy

深拷贝 deep copy 和 浅拷贝 shallow copy

浅拷贝 shallow copy
浅拷贝是指在复制过程中,只复制一层变量,不会复制深层
变量绑定的对象的复制过程
如:
	L = [3.1, 3.2]
	L1 = [1, 2, L]
	L2 = L1.copy()  # 浅拷贝
	print(L1)  # [1, 2, [3.1, 3.2]]
	print(L2)  # [1, 2, [3.1, 3.2]]
	L2[2][0] = 3.14
	print(L1)  # [1, 2, [3.14, 3.2]]
	print(L2)  # [1, 2, [3.14, 3.2]]

深拷贝 deep copy
如:
	import copy  # 导入复制模块
	L = [3.1, 3.2]
	L1 = [1, 2, L]
	L2 = copy.deepcopy(L1)  # 深拷贝
	print(L1)  # [1, 2, [3.1, 3.2]]
	print(L2)  # [1, 2, [3.1, 3.2]]
	L2[2][0] = 3.14
	print(L1)  # [1, 2, [3.1, 3.2]]
	print(L2)  # [1, 2, [3.14, 3.2]]
	[深浅拷贝的区别](https://www.cnblogs.com/huangbiquan/p/7795152.html)

猜你喜欢

转载自blog.csdn.net/qq_43494793/article/details/83447567
今日推荐