python shallow copy, deep copy

In Python references and assignments are different concepts, it requires in-depth understanding of shallow copy of python, deep copy, so as to not appear in the data structure used when the program runs misunderstanding led to problems.

1. assignment list, this is a reference.
>>> 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. Note that direct assignment and [:] difference (and learn the sort function):
>>> 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. shallow copy

Shallow copy copy only the outer layer, is also an element for variable objects are not copy
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. deep copy

Deep copy is more than the element itself is copied, but also open up some new memory space. Such data elements before subsequent copies will not affect operation.

>>> 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. expand understanding

Guess you like

Origin www.cnblogs.com/linnenn/p/11290543.html