python--Introduction Series (i) Index and slice

Source: Internet
Author: User

1. Indexes and slices : In Python, the index of an array is the same as in other languages, and from 0~n-1, the method of using the index is also in brackets, but the use of slices in Python simplifies the code

index : Take out the 3rd element in the array s: x=s[2]

slices : A method of using very little code to process an array element on demand. The slice has a minimum of 1 parameters, with a maximum of 3 parameters, as shown below:

Let's assume that the following array is declared as array=[2,3,4,5,6]

1 Parameters:

    1. array[1:]--the element from subscript 1 to the last element, returning [4,5,6]
    2. array[:3]--the element from subscript 0 to an element with subscript 2, excluding the element of subscript 3 , returns [2,3,4]
    3. array[::2]--starts with an element labeled 0, and the last one that has a subscript corresponding to the 0 index%2

Note : If any of the first 2 parameters are negative, then, in 2 cases, if the negative number is less than-N, then give negative values to 0, if only in-n~0, then it will be understood as 0~n-1 between the circle can be. The algorithm of negative circle, very simple, don't say

2 parameters:

    1. array[1:-2]--from subscript 1 to all elements before subscript (n-1), return [3,4,5]
    2. array[-2:3]--returns an empty array when the first argument is calculated to be greater than or equal to the last one []
    3. array[-13:3]--See the first note above, the first parameter is 0, return [2,3,4]

3 parameters

    1. [:: -1]--from beginning to end, reverse array, return [6,5,4,3,2]
    2. [ -1::-2]--from tail to head, each element, select an element, return [6,4,2]

Note : When there is no 3rd parameter, the slice can only be left -to-right, and an empty array is returned if the first parameter is greater than or equal to the second argument

Note : When the third parameter <0, the slice direction can be changed without the above limitation

python--Getting Started series (i) Indexes and slices

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.