[Numpy-discussion] Include last element when subindexing numpy arrays?

Matti Viljamaa mviljamaa at kapsi.fi
Wed Aug 31 08:34:54 EDT 2016


> On 31 Aug 2016, at 15:22, Robert Kern <robert.kern at gmail.com> wrote:
> 
> On Wed, Aug 31, 2016 at 12:28 PM, Matti Viljamaa <mviljamaa at kapsi.fi <mailto:mviljamaa at kapsi.fi>> wrote:
> >
> > 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,
> >
> > >>> A
> > array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
> > >>> A[0:5]
> > array([0, 1, 2, 3, 4])
> 
> A[5:]
> 
> --
> Robert Kern

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].

> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160831/017a0f6a/attachment.html>


More information about the NumPy-Discussion mailing list