Python slice with deep copy

A: deep and shallow copy copy:

1..a given a [:]

a [:] a deep copy, the assignment of the python, the address of the object is referenced by the assignment, a [:] is the content of the modified stack, the pointer is also directed to here means; and a shallow copy, modify, it opens up a new address space:

 

As shown below:

 

2.b = a && b = a[:] or b = a.copy()的区别;

b = a both point to the same object; b = a [:] / a.copy () are "shallow copy", copy only the outermost element, the inner element is nested by reference, rather than an independent distribution RAM.

 

>>> a = [1,2,['A','B']]
>>> b = a
>>> b[0]=9
>>> b[2][0]='d'
>>> a
[9, 2, ['d', 'B']]
>>> c = a[:]
>>> c[0] = 22
>>> c[2][0] = 'l'
>>> a
[9, 2, ['l', 'B']]
>>> b
[9, 2, ['l', 'B']]
>>> c
[22, 2, ['l', 'B']]

 

 

 

II. Array slice

Slicing base expression: Object [start_index: end_index: STEP] 
start_index, end_index, +/- STEP >>>

 

start_index, end_index, step may be both positive, the same is negative, the sign may also be used in combination. But must follow a principle, otherwise it is impossible to properly cut data: When start_index position when left end_index, showing from left to right value, then step must be positive (from left to right represent the same); when the position of start_index end_index is on the right, showing the values ​​from right to left, this time step must be negative (right to left represent the same), i.e., the order of the values ​​of both must be the same. For special cases, when start_index end_index or omitted, the starting index and ending index is determined by the positive and negative step, the value direction of the case does not exist there is a conflict (i.e., does not return empty list []), but to take positive and negative the result is totally different, because a left a right.

 


 

Guess you like

Origin www.cnblogs.com/dolphin-bamboo/p/11569458.html