Python slice thorough understanding

 

From:https://www.jianshu.com/p/15715d6f4dad

In the process of using the python solve practical problems, often encounter situations extracted from a certain part of the value of the object, it is designed for slicing 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

Comprising: positive and negative index index of two parts, as shown, to a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Example:

 
python indexing .jpg

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 cut when the data 'step ", and determines the sign of the" cut out direction ", n indicates" left to right "value, indicates a negative" from right to left "value. when the step is omitted, the default is 1, i.e., from left to right in increments of 1 value." cut orientation is important! "" Cut direction is very important! "" Cut direction is very important! "The important thing to say 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] >>> [1, 2, 3, 4, 5] step=1,从左往右取值,start_index=1到end_index=6同样表示从左往右取值。 
>>>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] >>> [0, 1, 2, 3, 4, 5] step=1,从左往右取值,从“起点”开始一直取到end_index=6。 
>>>a[:6:-1] >>> [9, 8, 7] step=-1,从右往左取值,从“终点”开始一直取到end_index=6。 
>>>a[6:] >>> [6, 7, 8, 9] step=1,从左往右取值,从start_index=6开始,一直取到“终点”。 
>>>a[6::-1] >>> [6, 5, 4, 3, 2, 1, 0] step=-1,从右往左取值,从start_index=6开始,一直取到“起点”。 
4. start_index end_index full and negative (-) in the case where the index
>>>a[-1:-6] >>> [] step=1,从左往右取值,而start_index=-1到end_index=-6决定了从右往左取值,两者矛盾,所以为空。 索引-1在-6的右边(如上图) 
>>>a[-1:-6:-1] >>> [9, 8, 7, 6, 5] step=-1,从右往左取值,start_index=-1到end_index=-6同样是从右往左取值。 索引-1在6的右边(如上图) 
>>>a[-6:-1] >>> [4, 5, 6, 7, 8] step=1,从左往右取值,而start_index=-6到end_index=-1同样是从左往右取值。 索引-6在-1的左边(如上图) 
>>>a[:-6] >>> [0, 1, 2, 3] step=1,从左往右取值,从“起点”开始一直取到end_index=-6。 
>>>a[:-6:-1] >>> [9, 8, 7, 6, 5] step=-1,从右往左取值,从“终点”开始一直取到end_index=-6。 
>>>a[-6:] >>> [4, 5, 6, 7, 8, 9] step=1,从左往右取值,从start_index=-6开始,一直取到“终点”。 
>>>a[-6::-1] >>> [4, 3, 2, 1, 0] step=-1,从右往左取值,从start_index=-6开始,一直取到“起点”。 
In the case of hybrid index - 5. start_index end_index and positive (+) and negative ()
>>>a[1:-6] >>> [1, 2, 3] start_index=1在end_index=-6的左边,因此从左往右取值,而step=1同样决定了从左往右取值,因此结果正确 
>>>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] >>> [9, 8, 7] start_index=-1在end_index=6的右边,因此从右往左取值,而step=-1同样决定了从右往左取值,因此结果正确。 
6. The operation of serial sections
>>>a[:8][2:5][-1:] >>> [4] 相当于: a[:8]=[0, 1, 2, 3, 4, 5, 6, 7] a[:8][2:5]= [2, 3, 4] a[:8][2:5][-1:] = 4 理论上可无限次连续切片操作,只要上一次返回的依然是非空可切片对象。 
7. The three parameters can be used slicing operation expression
>>>a[2+1:3*2:7%3] >>> [3, 4, 5] 即:a[2+1:3*2:7%3] = a[3:6:1] 
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:]: print(i) 就是利用range函数生成1-99的整数,然后取3的倍数,再取最后十个。 

Fourth, the common slicing operation

A list: a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for the description of 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)) >>>b = a[:] >>>b[0] = 9 #修改b的最外层元素,将1变成9 >>>b[2][0] = 'D' #修改b的内嵌层元素 >>>print('a={}'.format(a)) >>>print('b={}'.format(b)) >>>print('id(a)={}'.format(id(a))) >>>print('id(b)={}'.format(id(b))) a=[1, 2, ['A', 'B']] #原始a a=[1, 2, ['D', 'B']] #b修改内部元素A为D后,a中的A也变成了D,说明共享内部嵌套元素,但外部元素1没变。 b=[9, 2, ['D', 'B']] #修改后的b id(a)=38669128 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 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.

(B) when using a slice, the step of the positive and negative must be considered, especially when the step is omitted. Such as a [-1:], easily mistaken for the "destination" starts to get up "starting point", i.e. a [-1:] = [0, 1, 2, 3, 4, 5, 6, 7 , 8, 9], but in fact a [-1:] = a [-1] = 9, step = 1 because the value represented from left to right, while the start index start_index = -1 object itself is most elements of the right, then the right has no data, so that only a [-1] an element

Guess you like

Origin www.cnblogs.com/bsduan/p/11599719.html