Learn about slices in Python (detailed articles)

Slicing knowledge in Python.

In Python, slice (slice) is an advanced indexing method for sequence objects (such as list, string, tuple). Ordinary index only extracts the element corresponding to a subscript in the sequence, while slicing extracts the element corresponding to a range in the sequence. The range here is not a continuous segment in the narrow sense. The popular point is within a certain range. Cut out a part with a knife to reach the part you need.

1. The index method of slices

Take a = [1,2,3,4,5,6,7,8,9] as an example, positive and negative indexes

insert image description here

Second, the general way of indexing

A complete slice consists of three parameters and two colons ": ", used to separate the three parameters (start_index, end_index, step). When there is only one ":", the default third parameter step=1; when there is no ":", start_index=end_index, which means to cut the element specified by start_index.

切片操作的基本表达式:object[start:end:step]

start: the starting position of the slice, if there is no value, it starts from the beginning.
end: the end position of the slice, but does not include end (close before and open after), if there is no value, it means cutting to the end. step: step size,
default The value is 1. If the step size is positive, it means from left to right. Anyway, if it is negative, it means from right to left. The positive or negative of step determines the direction of tangent, which needs special attention!!!

3. Detailed cutting method of slicing operation

1. Cut a single value

>> a = [1,2,3,4,5,6]
>>> a[0] ##单个数,代表位数,第0位就是a里面的第一位
1
>>> a[5]##a里面的第5位,注意要从0作为第一位开始数
6

2. Cut the complete object

>>> b=[6,3,6,7,8,2,5,4]
>>> b[:] ##单独一个冒号,代表从头取到尾,步长默认为1
[6, 3, 6, 7, 8, 2, 5, 4]
>>> b[::]##单独两个冒号一样代表从头取到尾,步长默认为1
[6, 3, 6, 7, 8, 2, 5, 4]
>>> b[::-1]##注意,两个冒号后面是步长,步长为1,故应从右往左取
[4, 5, 2, 8, 7, 6, 3, 6]

3. When start and end are both positive numbers.

>>> a 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
>>> a[1:6] ##默认步长为1,从左往右,注意前闭后开
[1, 2, 3, 4, 5] 
>>> a[1:6:-1] 
[] ## 当取值的大小方向与步长的方向冲突时,返回值是空.
>>> a[6:1:-1] 
[6, 5, 4, 3, 2] 
>>> a[:6] ## 没有star代表从头开始取
[0, 1, 2, 3, 4, 5] 
>>> a[:6:-1] 
[9, 8, 7] 
>>> a[6:] 
[6, 7, 8, 9] 
>>> a[6::-1] 
[6, 5, 4, 3, 2, 1, 0] 

4. When start and end are all negative.

>>> a 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
>>> a[:-6] ## -6意思是从右往左数的第六位,故第六位是4,然后默认步长为1(从右往左),star未写,故从头开始取到4
[0, 1, 2, 3] 
>>> a[-1:-6] 
[]
>>> a[-1:-6:-1] 
[9, 8, 7, 6, 5] 
>>> a[-6:-1] ## 这个是从-6取到-1,步长为1,意思是从右往左数第6位到从右往左的第一位
[4, 5, 6, 7, 8] 
>>> a[:-6:-1] ## 这个是从0取到-6,步长为-1,因为开头是冒号,故起点被隐藏了
[9, 8, 7, 6, 5] 
>>> a[-6:] 
[4, 5, 6, 7, 8, 9] 
>>> a[-6::-1] ## 注意这个不等于[-6:-1],区别是这里是::(两个冒号),两个冒号后连接的是步长
[4, 3, 2, 1, 0]

5. When start and end are positive and negative mixed cases

>>> a 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
>>> a[1:-6] 
[1, 2, 3] 
>>> a[1:-6:-1] 
[]
>>> a[-1:6] 
[]
>>> a[-1:6:-1] 
[9, 8, 7] 

6. Continuous slice operation

>>> a 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
>>> a[:8][2:5][-1:] ## [:8]就是0取到8,在从其中取2到5,最后取-1位
[4] 
a[:8] ---- [0,1,2,3,4,5,6,7] 
[0,1,2,3,4,5,6,7][2:5]----[2,3,4] 
[2,3,4][-1:] ----[4] 

7. The three parameters in the slice are expressions

>>> a 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
>>> a[1+2:2*3:7%2]  ## 思路一样,运算出来,继续切
[3, 4, 5] 

8. Slices can manipulate other objects

>>> t = (1,2,3,4,5) 
>>> t[1:3] 
(2, 3) 
>>> s = "ACDRF" ##切片在字母上也是可以使用的,所以说切片很强大
>>> s[1:3] 
'CD' 
>>> (0, 1, 2, 3, 4, 5)[:3]#元组的切片操作
>>> (0, 1, 2)
>>> for i in range(0,100): 
... print(i) 
... 
>>> for i in range(0,100)[2::3][-10:]: ## 意思是从第二位开始取,步长为3,[-10]则是从倒数10位开始取,意思是去末尾10位.
... print(i) 
... 
71
74
77
80
83
86
89
92
95
98

Summarize:

1. If the slice operation is performed, no error will be reported if the subscript is exceeded

2. If the direction of the slice operation is contradictory, no error will be reported and the return will be empty

3. Reverse output list in python

​ The first type: loop The second type: [::-1] The third type: reverse()

Guess you like

Origin blog.csdn.net/Zombie_QP/article/details/125063501