Detailed Python slicing syntax

  • 2021-06-28 09:27:07
  • OfStack

Slices from Python are a particularly common feature used to value elements of a list.Using slices will also make your code look Pythonic-specific.

The main declaration of the slice is as follows, assuming there is now an list named alist:

alist = [0,1,2,3,4]

The basic form of slicing syntax is:

alist[start:stop:step]

You can see that there are three parameters for slicing a list:

start: Start position stop: End position step: Step

All three parameters are optional, meaning the subscript of list, index.The default value of the step parameter is 1.There are several forms of expression:

alist[start:stop]
alist[start:]
alist[:stop]
alist[:]

The start and stop parameters are specified in the first way, starting with the subscript specified by start and ending with stop-1, for example, a [1:3] yielding [1,2]. The second method starts with the subscript specified by start and takes the remaining alist elements.For example, a [1:] will get [1,2,3,4]. The third method takes the list element from the beginning of alist until the subscript is stop-1, for example, a[:4] yields the result [0,1,2,3] The fourth method returns the entire list if the start and stop parameters are not specified.

It should be noted that: stop denotes the first value that is not in the selected slice, and the value of alist [start:stop] is similar to the value range in mathematics [start, stop], so the result of start-stop is the number of elements taken when the step defaults to 1.

Negative usage

start and stop parameters are negative

The values of start and stop can be negative, indicating that the value starts at the end of list, not at the beginning.For example:

alist[-1]
alist[-3:]
alist[:-1]

[2,3,4]
[0,1,2,3]

step parameter is negative

When step is negative, it means reversing the list. The simplest example is as follows:

alist[::-1]

The output is [4,3,2,1,0].This example is actually returned as follows:

[alist[-1], alist[-1-step], ..., alist[0]]

That is, starting with the first element from the bottom, each step adds a step, because the step here is negative, so it is subtracted until the beginning of alist.Knowing this principle, you can understand some complex writings that add start and stop parameters, such as:

start parameter specified

alist[1::-1]

This is actually how you return:

[alist[1], alist[1-step], ..., alist[0]]

So, what's returned is [1,0]

stop parameter specified

alist[:1:-1]

This is actually how you return:

[alist[-1], alist[-1-step], ..., alist[stop+1]]

Therefore, the return is [4, 3, 2].

start and stop parameters are specified simultaneously

Note that the start parameter should be larger than the stop parameter because it is in reverse order.

alist[3:1:-1]

This is actually how you return:

[alist[start], alist[start-step], ..., alist[stop+1]]

So, what's returned is [3,2]

We can see that,

If the start parameter is specified, the element is taken from the start parameter index until the stop subscript or the first element of list is encountered. If the start parameter is not specified, it starts with the first element from the last to list until it encounters the stop subscript or the first element of list.

summary

The use of slicing syntax is particularly flexible and simplifies your code if you know it correctly.It is important to note that Python also provides the slice class for making slices, for example:

alist[start:stop:step]

Equivalent to

alist[(slice(start,stop,step))]

The above is the Python slice syntax introduced to you by this site. I hope it will be helpful to you. If you have any questions, please leave a message for me. This site will reply to you in time!


Related articles: