The function of double colon in Python [::]__python

Source: Internet
Author: User
the role of double colons in Python [::]

Python sequence Slice addresses can is written as A[start:end:step] and any of the start, stop or end can is drop PED.

The Python sequence slice address can be written as [Start: End: Step], where the start and end can be omitted

Range (n) to generate a [0,n) interval integer

Range (10)
[0,1,2,3,4,5,6,7,8,9]

Start start ellipsis, default starts with item No. 0

Range (10) [: 10:2]
[0,2,4,6,8]

When the end is omitted, the default to the array is final.

Range (10) [1::2]
[1,3,5,7,9]

When the beginning and the end are not omitted

Range [2:6:] #step省略默认为1
[2,3,4,5]

Range (10) [2:6:1]
[2,3,4,5]

Step Step=n, starting from Start (also counting) every step interval, take a number, until the end of

Range (20) [:: 3]
[0,3,6,9,12,15,18]

When step equals negative number, take the count from right to left.

Range (10) [::-1]
[9,8,7,6,5,4,3,2,1,0]

Range (10) [::-2]
[9,7,5,3,1]

———————————— –2017.7.4 ——————————————————— –

"The role of single quotes in Python"

A[start:end]: The range is [Start,end]

A=[[1,2,3],[4,5,6],[7,8,9]]
B=a[1:2]
[[4, 5, 6]]

A[:end]: The range is [0,end]

A=[[1,2,3],[4,5,6],[7,8,9]]
B=A[:1]
[[1, 2, 3]]

A[start:]: range from start to last (including last)

A=[[1,2,3],[4,5,6],[7,8,9]]
B=a[1:]
[[4, 5, 6], [7, 8, 9]]

Related Article

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.