- 浅拷贝
>>> L = [3.1,3.2]
>>> L1 = [1,2,L]
>>> L2 = L1.copy()
>>> L1
[1, 2, [3.1, 3.2]]
>>> L2
[1, 2, [3.1, 3.2]]
>>> L2[1] = 3
>>> L1
[1, 2, [3.1, 3.2]]
>>> L2
[1, 3, [3.1, 3.2]]
>>> L2[2][0] = 3.14
>>> L1
[1, 2, [3.14, 3.2]]
>>> L2
[1, 3, [3.14, 3.2]]
>>> L[1] = 1111
>>> L1
[1, 2, [3.14, 1111]]
>>> L2
[1, 3, [3.14, 1111]]
>>>
2.深拷贝
>>> import copy
>>> L = [3.1,3.2]
>>> L1 = [1,2,L]
>>> L2 = copy.deepcopy(L1)
>>> L1
[1, 2, [3.1, 3.2]]
>>> L2
[1, 2, [3.1, 3.2]]
>>> L2[2][0] = 3.14
>>> L1
[1, 2, [3.1, 3.2]]
>>> L2
[1, 2, [3.14, 3.2]]
>>> L[1] = 222222
>>> L1
[1, 2, [3.1, 222222]]
>>> L2
[1, 2, [3.14, 3.2]]
浅拷贝虽然说是拷贝,但也是 “ 身不由己 ” ,当改除了一层之外的值时,都会改动(这时不管是不是拷贝的,都改,看浅拷贝的栗子就知道了吧,内嵌列表表面上也拷贝过来了,但实际还是不是自己说了算的,只要改了内嵌列表的值,拷贝的也要改,这就是只拷贝一层,内嵌的就无能为力了)
深拷贝就是彻底的拷贝,两者就再毫无关系,虽然拷贝完不改的话长的一样,但是不管对谁改动,另一个也是毫不受影响,详细看深拷贝。
网友评论