Slicing operations
The subscript operation is used for the area cutting of ordered series.
The most common slicing operations are the list (the other ordered series, such as the slicing operations of tuple and STR, are the same as list):
Lst[start:end:step]
Start: Start index, end: Ending index, step step
The range of values for the slice operation, the element that contains the starting index, and the element that does not contain the end index
Positive index, starting from 0, counting from left to right
Negative index, starting from 1, counting from right to left
Example: If step (step) is the default (1) or other positive integer, the slice order is left to right, and start (the starting index) must precede the end index, otherwise an empty list is obtained.
>>> LST
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> Lst[0:8]
[0, 1, 2, 3, 4, 5, 6, 7]
>>> Lst[0:-2]
[0, 1, 2, 3, 4, 5, 6, 7]
>>> Lst[-8:6]
[2, 3, 4, 5]
>>> Lst[-2:3]
[]
>>> Lst[7:2]
[]
>>> Lst[-2:-5]
[]
Example: If step (step) is negative, the slice order is from right to left and start (start index) must be after end (end index), otherwise an empty list will be obtained.
The returned results are in reverse order relative to the original list.
>>> LST
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> Lst[7:2:-1]
[7, 6, 5, 4, 3]
>>> Lst[-1::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> Lst[-1:-5:-1]
[9, 8, 7, 6]
>>> Lst[-1:-7:2]
[]
>>>
Example: When step is the default (1) or-1, the slice count is the normal order value, when greater than 1, or less than 1, according to the step jump value, the step is 2 or 2 is the first bit in the two-bit element, the second bit discarded, and so on.
>>> Lst[::2]
[0, 2, 4, 6, 8]
>>> Lst[::3]
[0, 3, 6, 9]
>>> Lst[-1::-2]
[9, 7, 5, 3, 1]
>>> Lst[-1::-3]
[9, 6, 3, 0]
Instance: When start or end exceeds the index range of LST, start < index range first bit, End > Index range Last, start takes value from first position of index, end is Len (LST)
>>> Lst[-100:5]
[0, 1, 2, 3, 4]
>>> lst[2:100]
[2, 3, 4, 5, 6, 7, 8, 9]
>>> lst[-100:100]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Example: Start is 0 can be omitted, end is the length of the list can be omitted, equivalent to the Lst.copy () method
>>> LST
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lst[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> Lst[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>>
2016.07.09-10 Slicing operations