Ndarray array slicing problem in Python a[-n-x:-y]

Source: Internet
Author: User

First look at the following code:

>>a=np.arange (10)
>>a
Array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>A[-7]
Array ([3, 4, 5, 6, 7, 8, 9])
>>A[-7-1:-1]
Array ([2, 3, 4, 5, 6, 7, 8])
>>a[-7-1:-2]
Array ([2, 3, 4, 5, 6, 7])
>>A[-7-2:-1]
Array ([1, 2, 3, 4, 5, 6, 7, 8])
>> A[-7-2:]
Array ([1, 2, 3, 4, 5, 6, 7, 8, 9])

As can be seen from the above example, A[-n] is to select the last n elements of a array to form an array. A[-N-X:-Y] indicates that x elements are taken forward on the formed A[-n] array, and that the Y element is less than the backward one. Like what:

A[-7-1:-2]=[2, 3, 4, 5, 6, 7], this is the A[-7]=[3, 4, 5, 6, 7, 8, 9] on the basis of a number of forward more than a few 2, backward take two number 8, 9, the formation of a new array.

Ndarray array slicing problem in Python a[-n-x:-y]

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.