Thoroughly get to know Python slicing operations

 
  In the process of using the Python solve practical problems, often we encounter situations extract some value from an object, the slicing operation it is dedicated to a powerful weapon to complete this operation. In theory, as long as the conditional expression proper, may be achieved by any single or multiple target slicing cut. Basic syntax slicing operation is relatively simple, but not completely clear if the internal logic is also very prone to error, and this error sometimes deeper than hidden, difficult to detect. This article summarizes a detailed example of the pellets to various operations. If errors and inadequacies please correct me Daniel!


First, indexing can slice objects Python

Python be sliced index object comprises: a positive index and negative indices of two parts.
As shown below, in a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]an example:


Second, the general mode of operation of the slice Python

A complete expression contains two slice ":", for separating the three parameters (start_index, end_index, step), when only a ":", the default parameters of the third step = 1.

切片操作基本表达式:object[start_index : end_index : step]

STEP : positive and negative numbers are available, the absolute value of which determines the "step" at the cut data, and the sign determined "cut direction" , n indicates "left to right" value, negative for "right left "value. When the step is omitted, the default is 1, i.e., from left to right an incremental value. " Cut direction is very important !" " Cut direction is very important !" " Cut direction is very important !", Said the important thing three times!

start_index : indicating a start index (including the index itself); Omit this argument, from the object "endpoint" start value, as is the "starting point" or the "destination" starts, the positive and negative parameters determined by step, is a positive step from the "starting point" start from a negative "end" began.

end_index : indicates termination index (the index itself is not included); when the argument is omitted, taking data up indicates "end", as is the "starting point" or the "end point", the same positive and negative parameter determined by step, to step timing until the "end" is negative until the "starting point."


Three, Python detailed example of slicing operation

The following examples are list a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Example:
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

1. cut single value
>>> a[0]
0
>>> a[-4]
6
2. cut the complete object
>>> a[:] # 从左往右
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[::] # 从左往右
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[::-1] # 从右往左
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
3.start_index and end_index are all positive (+) index case
>>> a[1:6] # step=1,从左往右取值,start_index=1到end_index=6同样表示从左往右取值。
[1, 2, 3, 4, 5]

>>>a[1:6:-1] # step=-1,决定了从右往左取值,而start_index=1到end_index=6决定了从左往右取值,两者矛盾。
>>> [] # 输出为空列表,说明没取到数据。

>>>a[6:1] # step=1,决定了从左往右取值,而start_index=6到end_index=1决定了从右往左取值,两者矛盾。
>>> [] # 同样输出为空列表。

>>>a[:6] # step=1,从左往右取值,从“起点”开始一直取到end_index=6。
>>> [0, 1, 2, 3, 4, 5]

>>>a[:6:-1] # step=-1,从右往左取值,从“终点”开始一直取到end_index=6。
>>> [9, 8, 7]

>>>a[6:] # step=1,从左往右取值,从start_index=6开始,一直取到“终点”。
>>> [6, 7, 8, 9]

>>>a[6::-1] # step=-1,从右往左取值,从start_index=6开始,一直取到“起点”。
>>> [6, 5, 4, 3, 2, 1, 0]
4.start_index and end_index are all negative (-) index case
>>>a[-1:-6] # step=1,从左往右取值,而start_index=-1到end_index=-6决定了从右往左取值,两者矛盾。
>>> []

>>>a[-1:-6:-1] # step=-1,从右往左取值,start_index=-1到end_index=-6同样是从右往左取值。
>>> [9, 8, 7, 6, 5]

>>>a[-6:-1] # step=1,从左往右取值,而start_index=-6到end_index=-1同样是从左往右取值。
>>> [4, 5, 6, 7, 8]

>>>a[:-6] # step=1,从左往右取值,从“起点”开始一直取到end_index=-6。
>>> [0, 1, 2, 3]

>>>a[:-6:-1] # step=-1,从右往左取值,从“终点”开始一直取到end_index=-6。
>>> [9, 8, 7, 6, 5]

>>>a[-6:] # step=1,从左往右取值,从start_index=-6开始,一直取到“终点”。
>>> [4, 5, 6, 7, 8, 9]

>>>a[-6::-1] # step=-1,从右往左取值,从start_index=-6开始,一直取到“起点”。
>>> [4, 3, 2, 1, 0]
In the case of hybrid index - 5.start_index end_index and positive (+) and negative ()
>>>a[1:-6] # start_index=1在end_index=-6的左边,因此从左往右取值,而step=1同样决定了从左往右取值。
>>> [1, 2, 3]

>>>a[1:-6:-1] # start_index=1在end_index=-6的左边,因此从左往右取值,但step=-则决定了从右往左取值,两者矛盾。
>>> []

>>>a[-1:6] # start_index=-1在end_index=6的右边,因此从右往左取值,但step=1则决定了从左往右取值,两者矛盾。
>>> []

>>>a[-1:6:-1] # start_index=-1在end_index=6的右边,因此从右往左取值,而step=-1同样决定了从右往左取值。
>>> [9, 8, 7]
6. The operation of serial sections
>>>a[:8][2:5][-1:]
>>> [4]

Equivalent to:
A [:. 8] = [0,. 1, 2,. 3,. 4,. 5,. 6,. 7]
A [:. 8] [2:. 5] = [2,. 3,. 4]
A [:. 8] [ 2: 5] [- 1:] = 4
theoretically unlimited successive slicing operations, as long as the last non-empty return is still an object can be sliced.

7. The three parameters can be used slicing operation expression
>>>a[2+1:3*2:7%3] # 即:a[2+1:3*2:7%3] = a[3:6:1]
>>> [3, 4, 5]
8. Other objects slicing

Slicing the foregoing description are to be described as an example list, but the data slice types of operations that may be performed in fact there are many, including a tuple, string and the like.

>>> (0, 1, 2, 3, 4, 5)[:3] # 元组的切片操作
>>> (0, 1, 2)

>>>'ABCDEFG'[::2] # 字符串的切片操作
>>>'ACEG'

>>>for i in range(1,100)[2::3][-10:]: # 利用range函数生成1-99的整数,然后取3的倍数,再取最后十个。
       print(i, end=' ')
>>> 72 75 78 81 84 87 90 93 96 99

Four, Python common slicing operation

In a list: a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]To illustrate the object

1. Take even positions
>>>b = a[::2]
[0, 2, 4, 6, 8]
2. Take odd positions
>>>b = a[1::2]
[1, 3, 5, 7, 9]
3. Copy the entire object
>>>b = a[:] # ★★★★★
>>>print(b) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>print(id(a)) # 41946376
>>>print(id(b)) # 41921864

>>>b = a.copy()
>>>print(b) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>print(id(a)) # 39783752
>>>print(id(b)) # 39759176

Note that: [:] and .copy () are "shallow copy", copy only the outermost element, the inner element is nested by reference, rather than separate memory allocation.

>>>a = [1,2,['A','B']]
>>>print('a={}'.format(a))
a=[1, 2, ['A', 'B']] # 原始a

>>>b = a[:]
>>>b[0] = 9 # 修改b的最外层元素,将1变成9
>>>b[2][0] = 'D' # 修改b的内嵌层元素
>>>print('a={}'.format(a)) # b修改内部元素A为D后,a中的A也变成了D,说明共享内部嵌套元素,但外部元素1没变。
a=[1, 2, ['D', 'B']] 

>>>print('b={}'.format(b)) # 修改后的b
b=[9, 2, ['D', 'B']] 

>>>print('id(a)={}'.format(id(a)))
id(a)=38669128

>>>print('id(b)={}'.format(id(b)))
id(b)=38669192
4. Modify the individual elements
>>>a[3] = ['A','B']
[0, 1, 2, ['A', 'B'], 4, 5, 6, 7, 8, 9]
5. Insert the element in a certain position
>>>a[3:3] = ['A','B','C']
[0, 1, 2, 'A', 'B', 'C', 3, 4, 5, 6, 7, 8, 9]
>>>a[0:0] = ['A','B']
['A', 'B', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
6. Alternatively part of the element
>>>a[3:6] = ['A','B']
[0, 1, 2, 'A', 'B', 6, 7, 8, 9]

V. Summary

(A) start_index, end_index, STEP may be both positive, the same is negative, the sign may also be used in combination. But we must follow the principle that both the value of the order must be the same , 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 ( similarly represented from left to right); and when the right position end_index start_index, showing the values from right to left, this time step must be negative (right to left similarly represented). 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 , but to take positive and negative result is completely different, as a a left to right.
(B) when using a slice, the step of the positive and negative must be considered, especially when the step is omitted. For example a[-1:], it is easy to mistakenly believe that the "end" the beginning has been to take the "starting point", that is a[-1:]= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], in fact a[-1:]=a[-1]=9, because the step = 1 represents the value from left to right, and the starting index start_index = -1 is itself the object most of the elements on the right, and then did not have the right data, so only a[-1]one element.

 
 
 
After slightly modifying the original
original author: Maldives Maldives
description link: https://www.jianshu.com/p/15715d6f4dad

Guess you like

Origin www.cnblogs.com/malinqing/p/11272485.html