Include last element when subindexing numpy arrays?
![](https://secure.gravatar.com/avatar/0211fdd567c72ea9d612aa93b2cf121f.jpg?s=120&d=mm&r=g)
Is there a clean way to include the last element when subindexing numpy arrays? Since the default behaviour of numpy arrays is to omit the “stop index”. So for,
![](https://secure.gravatar.com/avatar/0211fdd567c72ea9d612aa93b2cf121f.jpg?s=120&d=mm&r=g)
No that returns the subarray starting from index 5 to the end. What I want to be able to return array([0, 1, 2, 3, 4, 5]) (i.e. last element 5 included) but without the funky A[0:6] syntax, which looks like it should return array([0, 1, 2, 3, 4, 5, 6]) but since bumpy arrays omit the last index, returns array([0, 1, 2, 3, 4, 5]) which syntactically would be more reasonable to be A[0:5].
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
On Wed, Aug 31, 2016 at 1:34 PM, Matti Viljamaa <mviljamaa@kapsi.fi> wrote:
On 31 Aug 2016, at 15:22, Robert Kern <robert.kern@gmail.com> wrote:
On Wed, Aug 31, 2016 at 12:28 PM, Matti Viljamaa <mviljamaa@kapsi.fi>
wrote:
Ah, I see what you are asking now. The answer is "no"; this is just the way that slicing works in Python in general. numpy merely follows suit. It is something that you will get used to with practice. My sense of "funkiness" and "reasonableness" is the opposite of yours, for instance. -- Robert Kern
![](https://secure.gravatar.com/avatar/0211fdd567c72ea9d612aa93b2cf121f.jpg?s=120&d=mm&r=g)
No that returns the subarray starting from index 5 to the end. What I want to be able to return array([0, 1, 2, 3, 4, 5]) (i.e. last element 5 included) but without the funky A[0:6] syntax, which looks like it should return array([0, 1, 2, 3, 4, 5, 6]) but since bumpy arrays omit the last index, returns array([0, 1, 2, 3, 4, 5]) which syntactically would be more reasonable to be A[0:5].
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
On Wed, Aug 31, 2016 at 1:34 PM, Matti Viljamaa <mviljamaa@kapsi.fi> wrote:
On 31 Aug 2016, at 15:22, Robert Kern <robert.kern@gmail.com> wrote:
On Wed, Aug 31, 2016 at 12:28 PM, Matti Viljamaa <mviljamaa@kapsi.fi>
wrote:
Ah, I see what you are asking now. The answer is "no"; this is just the way that slicing works in Python in general. numpy merely follows suit. It is something that you will get used to with practice. My sense of "funkiness" and "reasonableness" is the opposite of yours, for instance. -- Robert Kern
participants (2)
-
Matti Viljamaa
-
Robert Kern