乐趣区

python浅拷贝深拷贝

Python 中赋值和引用是不同的概念,需要深入理解 python 中的浅拷贝,深拷贝,这样才能在使用的时候不至于出现数据结构理解错误导致程序运行出现问题。

1. 列表的赋值,这是引用。
>>> a = [1,2,3,4,5,6,7]
>>> c = a
>>> c
[1, 2, 3, 4, 5, 6, 7]
>>> c.append(33)
>>> c
[1, 2, 3, 4, 5, 6, 7, 33]
>>> a
[1, 2, 3, 4, 5, 6, 7, 33]
>>> c.pop()
33
>>> c
[1, 2, 3, 4, 5, 6, 7]
>>> a
[1, 2, 3, 4, 5, 6, 7]
2. 注意直接赋值和 [:] 的区别(并学习 sort 函数):
>>> a
[1, 2, 3, 4, 5, 6, 7]
>>> c
[1, 2, 3, 4, 5, 6, 7]
>>> d
[1, 2, 3, 4, 5, 6, 7]
>>> id(a),id(c),id(d)
(4673312584, 4673312584, 4673314632)
>>> a.sort()
>>> a
[1, 2, 3, 4, 5, 6, 7]
>>> a.sort(reverse=False)
>>> a
[1, 2, 3, 4, 5, 6, 7]
>>> a.sort(reverse=True)
>>> a
[7, 6, 5, 4, 3, 2, 1]
>>> c
[7, 6, 5, 4, 3, 2, 1]
>>> d
[1, 2, 3, 4, 5, 6, 7]
3. 浅拷贝

浅拷贝只能拷贝外层,就是对于元素也是可变对象是不会拷贝
eg:

>>> import copy
>>> a = [1,2,3,4,5,6,7]
>>> b = [11,22,33,44,55,66,77]
>>> a
[1, 2, 3, 4, 5, 6, 7]
>>> b
[11, 22, 33, 44, 55, 66, 77]
>>> c = a
>>> c
[1, 2, 3, 4, 5, 6, 7]
>>> d = [a,b]
>>> d
[[1, 2, 3, 4, 5, 6, 7], [11, 22, 33, 44, 55, 66, 77]]
>>> a.append(33)
>>> a
[1, 2, 3, 4, 5, 6, 7, 33]
>>> b
[11, 22, 33, 44, 55, 66, 77]
>>> c
[1, 2, 3, 4, 5, 6, 7, 33]
>>> d
[[1, 2, 3, 4, 5, 6, 7, 33], [11, 22, 33, 44, 55, 66, 77]]
>>> id(a),id(b),id(c),id(d)
(4671685064, 4670406408, 4671685064, 4673337544)
>>> e = copy.copy(a)
>>> id(e)
4673311752
>>> e
[1, 2, 3, 4, 5, 6, 7, 33]
>>> e.append(44)
>>> e
[1, 2, 3, 4, 5, 6, 7, 33, 44]
>>> a
[1, 2, 3, 4, 5, 6, 7, 33]
>>> b
[11, 22, 33, 44, 55, 66, 77]
>>> c
[1, 2, 3, 4, 5, 6, 7, 33]
>>> d
[[1, 2, 3, 4, 5, 6, 7, 33], [11, 22, 33, 44, 55, 66, 77]]
>>> e
[1, 2, 3, 4, 5, 6, 7, 33, 44]
>>> a
[1, 2, 3, 4, 5, 6, 7, 33]
>>> a.append(55)
>>> a
[1, 2, 3, 4, 5, 6, 7, 33, 55]
>>> b
[11, 22, 33, 44, 55, 66, 77]
>>> c
[1, 2, 3, 4, 5, 6, 7, 33, 55]
>>> d
[[1, 2, 3, 4, 5, 6, 7, 33, 55], [11, 22, 33, 44, 55, 66, 77]]
>>> e
[1, 2, 3, 4, 5, 6, 7, 33, 44]
>>> id(a),id(b),id(c),id(d),id(e)
(4671685064, 4670406408, 4671685064, 4673337544, 4673311752)
>>> d
[[1, 2, 3, 4, 5, 6, 7, 33, 55], [11, 22, 33, 44, 55, 66, 77]]
>>> a
[1, 2, 3, 4, 5, 6, 7, 33, 55]
>>> c
[1, 2, 3, 4, 5, 6, 7, 33, 55]
>>> e
[1, 2, 3, 4, 5, 6, 7, 33, 44]
>>> f = copy.copy(d)
>>> id(f)
4673326664
>>> id(a),id(b),id(c),id(d),id(e),id(f)
(4671685064, 4670406408, 4671685064, 4673337544, 4673311752, 4673326664)
>>> f
[[1, 2, 3, 4, 5, 6, 7, 33, 55], [11, 22, 33, 44, 55, 66, 77]]
>>> d
[[1, 2, 3, 4, 5, 6, 7, 33, 55], [11, 22, 33, 44, 55, 66, 77]]
>>> a
[1, 2, 3, 4, 5, 6, 7, 33, 55]
>>> a.append(66)
>>> a
[1, 2, 3, 4, 5, 6, 7, 33, 55, 66]
>>> d
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77]]
>>> f
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77]]
>>> id(f[0])
4671685064
>>> id(d[0])
4671685064
>>> id(a)
4671685064
>>> id(c)
4671685064
>>> e
[1, 2, 3, 4, 5, 6, 7, 33, 44]
>>> a
[1, 2, 3, 4, 5, 6, 7, 33, 55, 66]
>>> c
[1, 2, 3, 4, 5, 6, 7, 33, 55, 66]
>>> f[0]
[1, 2, 3, 4, 5, 6, 7, 33, 55, 66]
>>> e[0]
1
>>> d[0]
[1, 2, 3, 4, 5, 6, 7, 33, 55, 66]
>>> d
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77]]
>>> f
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77]]
>>> d.append(000)
>>> d
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77], 0]
>>> f
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77]]
>>> 
>>> d.pop()
0
>>> d
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77]]
>>> f
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77]]
4. 深拷贝

深拷贝就是不止元素本身会被拷贝,同时也会开辟一段新的内存空间。这样操作拷贝之后的元素也不会影响之前的数据。

>>> dd = copy.deepcopy(d)
>>> d
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77]]
>>> dd
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77]]
>>> id(d),id(f),id(dd)
(4673337544, 4673326664, 4673312520)
>>> dd
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77]]
>>> a
[1, 2, 3, 4, 5, 6, 7, 33, 55, 66]
>>> c
[1, 2, 3, 4, 5, 6, 7, 33, 55, 66]
>>> d
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77]]
>>> f
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77]]
>>> e
[1, 2, 3, 4, 5, 6, 7, 33, 44]
>>> dd
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77]]
>>> a.pop()
66
>>> a
[1, 2, 3, 4, 5, 6, 7, 33, 55]
>>> c
[1, 2, 3, 4, 5, 6, 7, 33, 55]
>>> d
[[1, 2, 3, 4, 5, 6, 7, 33, 55], [11, 22, 33, 44, 55, 66, 77]]
>>> f
[[1, 2, 3, 4, 5, 6, 7, 33, 55], [11, 22, 33, 44, 55, 66, 77]]
>>> e
[1, 2, 3, 4, 5, 6, 7, 33, 44]
>>> dd
[[1, 2, 3, 4, 5, 6, 7, 33, 55, 66], [11, 22, 33, 44, 55, 66, 77]]
>>> id(dd)
4673312520
>>> id(dd[0])
4673312392
>>> 
>>> id(a),id(b),id(c),id(d),id(e),id(f),id(dd[0])
(4671685064, 4670406408, 4671685064, 4673337544, 4673311752, 4673326664, 4673312392)
>>> id(d[0])
4671685064
>>> id(id(e[0])
... 
KeyboardInterrupt
>>> id(f[0])
4671685064
>>> hh = (1,2,3,4,5)
>>> ii = copy.copy(hh)
>>> id(hh),id(ii)
(4675125760, 4675125760)
>>> jj = copy.copy(hh)
>>> hh,ii,jj
((1, 2, 3, 4, 5), (1, 2, 3, 4, 5), (1, 2, 3, 4, 5))
>>> id(jj)
4675125760
>>> a = '1213131'
>>> id(a)
4675118840
>>> aa = copy.copy(a)
>>> aa
'1213131'
>>> id(aa)
4675118840
>>> bb = copy.deepcopy(a)
>>> id(bb)
4675118840
1. 拓展理解
退出移动版