Basic concept
Python slicing is a particularly common feature used to evaluate elements of a list. Using slicing also makes your code look Pythonic.
The main declaration of the slice is as follows, assuming we now have alist named alist:
Alist =,1,2,3,4 [0]Copy the code
The basic form of slice grammar is:
alist[start:stop:step]
Copy the code
It can be seen that there are three parameters for the slicing operation of the list, respectively:
- Start: indicates the start position
- Stop: indicates the stop position
- Step: step length
The three parameters are optional and have the subscript of the list, that is, index. The default value of the step parameter is 1. There are the following forms of expression:
alist[start:stop]
alist[start:]
alist[:stop]
alist[:]
Copy the code
- The first method specifies the start and stop parameters, taking the alist element from the index specified by start until stop-1, for example
a[1:3]
As the result of the[1, 2]
. - The second method starts at the index specified by start and takes the remainder of the alist element. For example,
a[1:]
Will get[1, 2, 3, 4]
. - The third way will start at alist and take the list element until the index is stop-1, for example
a[:4]
I’ll get the result,1,2,3 [0]
- The fourth method does not specify start and stop parameters and returns the entire list.
It should be noted that :stop represents the first value not in the selected slice. The value of alist[start:stop] is similar to the value range of [start, stop) in mathematics, so when the default step size is 1, the result of start-stop is the number of elements taken.
Negative usage
Start and stop parameters are negative values
Start and stop can be negative values, indicating that values start at the end of the list, not the beginning. Such as:
alist[-1]
alist[-3:]
alist[:-1]
Copy the code
- The first way is to take the last value of alist and output 4.
- The second way to write it is to take the rest of the list from the third to last value of alist, and print
[4] 2
. - The third way to write it is to take the elements in the list from the start of alist until the index is the reciprocal value, and the output is
,1,2,3 [0]
.
The step parameter is negative
If step is negative, reverse the list. The simplest example is as follows:
alist[::-1]
Copy the code
The output is [4,3,2,1,0]. This example actually returns the following form,
[alist[-1], alist[-1-step], ..., alist[0]]
Copy the code
That is, starting from the last element, add the step size to each step, because the step size is negative, so subtraction, until the beginning of Alist. With this in mind, you can understand some complicated ways of writing things with start and stop parameters, such as:
The start argument is specified
alist[1::-1]
Copy the code
It actually returns this:
[alist[1], alist[1-step], ..., alist[0]]
Copy the code
So, it returns [1,0]
The stop argument is specified
alist[:1:-1]
Copy the code
It actually returns this:
[alist[-1], alist[-1-step], ..., alist[stop+1]]
Copy the code
Therefore, [4, 3, 2] is returned.
Both start and stop parameters are specified
Note that the start argument should be greater than the stop argument because it is in reverse order.
alist[3:1:-1]
Copy the code
It actually returns this:
[alist[start], alist[start-step], ..., alist[stop+1]]
Copy the code
So, it returns [3,2]
As we can see,
- If the start parameter is specified, elements are fetched from the start parameter index until either the stop index or the first element of the list is encountered.
- If the start argument is not specified, it starts from the last element of the list until either the stop index or the first element of the list is encountered.
conclusion
Slicing syntax is extremely flexible and can simplify your code if used correctly. Note that Python also provides a slice class for making slices, for example:
alist[start:stop:step]
Copy the code
It’s actually equivalent to
alist[(slice(start,stop,step))]
Copy the code