Python: Slice character

Source: Internet
Author: User

Python's slice Character: (:) 1, single-slice character

Python's slice characters are used for tuples, strings, or lists, using left-hand open, which contains the nth number on the left side of the colon, not the number on the right, and the following as an example of a column:

>>> a=[1,2,3,4]>>> a[1:][2, 3, 4]>>> a[1:3][2, 3]>>> a[:3][1, 2, 3]>>>
2. Double-slice characters

Double slices: (::) is generally used to take a few of the elements or a set of strings, such as a list, the wording is also very flexible, the following explanation:

1) [:: N],n can be positive or negative, minus is the right-to-left, the absolute value of N represents how many to take one, the following is an example of a string:

>>> a="ABCDEFG">>> a[::-1]'GFEDCBA'>>> a[::-2]'Geca'>>> A[::2]'Aceg'>>> A[::3]'ADG'>>> A[::6]'AG'>>> A[::9]'a'
2) [M::n],m, N can be positive or negative, the following specific implementation:

I, when N is positive, is considered [m:][::n], as follows:

>>> a= (1,2,3,4,5,6,7) >>> A[3::2] (4, 6) >>> A[3:][::2] (4, 6)

2, when N is negative, as the first M-bit in the opposite direction of all values (including the first M bit), and then the [:: N] Operation

>>> a= (1,2,3,4,5,6,7) >>> A[4::-2] (5, 3, 1)

The above is the No. 0 to 4th bit of a is taken out, the result is (1,2,3,4,5), and then the [:: N] operation, the end result is (5,3,1), M is negative when the same, here is not added to the

3) [m:n:k] M, N can be positive or negative, K is positive:
>>> a= (1,2,3,4,5,6,7)>>> a[2:-2:3] (3,)

A take [2:-2], the result is (3,4,5), then every 3 to take one, the result is (3,) as for the following comma, is to indicate that this is a tuple, rather than a separate 3

3) [m:n:k] M, N can be positive or negative, K is negative:
>>> a=[1,2,3,4,5,6,7]>>> a[6:1:-2[7, 5, 3]>>>

When the third position is negative, take the first two opposite numbers, that is, take 1 to 6 bits, but because 6 is closed, 1 is open, so the result is [3,4,5,6,7], and then again [:: 2], the result is [7,5,3]

The rest will not be discussed.

Python: Slice character

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.