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 slice extracts the element corresponding to a range in the sequence, and 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
Taking a = [1,2,3,4,5,6,7,8,9] as an example, positive and negative indices
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 will start 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 the end of the slice.
step: step size, the default 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 cutting direction, 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] ##Single number, representing the number of digits, the 0th digit is the first digit in a
1
>>> a[5]##a The 5th digit, pay attention to start counting from 0 as the first digit
6
1
2
3
4
5
2. Cut the complete object
>>> b=[6,3,6,7,8,2,5,4]
>>> b[:] ##A single colon means taking from the beginning to the end, and the default step size is 1
[6, 3, 6, 7, 8, 2, 5, 4]
>>> b[::]##Single two colons represent from the beginning to the end, the default step size is 1
[6, 3, 6, 7, 8, 2, 5, 4]
>>> b[::-1]##Attention, after the two colons is the step size, the step size is 1, so it should be taken from right to left
[4, 5, 2, 8, 7, 6, 3, 6]
1
2
3
4
5
6
7
3. When start and end are both positive numbers.
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[1:6] ##The default step size is 1, from left to right, pay attention to open before closing
[1, 2, 3, 4, 5]
>>> a[1:6:-1]
[] ## When the size direction of the value conflicts with the direction of the step size, the return value is empty.
>>> a[6:1:-1]
[6, 5, 4, 3, 2]
>>> a[:6] ## No star means to start from the beginning
[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]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
4. When start and end are all negative.
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[:-6] ## -6 means the sixth digit from right to left, so the sixth digit is 4, and the default step size is 1 (from right to left), star is not written, so Take from scratch to 4
[0, 1, 2, 3]
>>> a[-1:-6]
[]
>>> a[-1:-6:-1]
[9, 8, 7, 6, 5]
>>> a[-6:-1] ## This is taken from -6 to -1, with a step size of 1, meaning the 6th digit from right to left to the first digit from right to left
[4, 5, 6, 7, 8]
>>> a[:-6:-1] ## This is from 0 to -6, the step size is -1, because the beginning is a colon, so the starting point is hidden
[9, 8, 7, 6, 5]
>>> a[-6:]
[4, 5, 6, 7, 8, 9]
>>> a[-6::-1] ## Note that this is not equal to [-6:-1], the difference is that here is :: (two colons), and the connection between the two colons is the step size
[4, 3, 2, 1, 0]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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]
1
2
3
4
5
6
7
8
9
10
6. Continuous slice operation
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[:8][2:5][-1:] ## [:8] is 0 to 8, from which 2 to 5, and finally -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]
1
2
3
4
5
6
7
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] ## The idea is the same, calculate it, continue to cut
[3, 4, 5]
8. Slices can manipulate other objects
>>> t = (1,2,3,4,5)
>>> t[1:3]
(twenty three)
>>> s = “ACDRF” ##Slices can also be used on letters, so slices are very powerful
>>> s[1:3]
‘CD’
>>> (0, 1, 2, 3, 4, 5)[:3]#Tuple slice operation
>>> (0, 1, 2)
>>> for i in range(0,100):
…print(i)
…
>>> for i in range(0,100)[2::3][-10:]: ## means to start from the second digit, with a step size of 3, and [-10] means to fetch from the last 10 digits , which means go to the last 10 digits.
…print(i)
…
71
74
77
80
83
86
89
92
95
98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
twenty two
twenty three
twenty four
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()