2016.07.09-10 Slicing operations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.