Discussion:
[Numpy-discussion] Include last element when subindexing numpy arrays?
Matti Viljamaa
2016-08-31 11:28:18 UTC
Permalink
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])
Robert Kern
2016-08-31 12:22:54 UTC
Permalink
Post by Matti Viljamaa
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
Matti Viljamaa
2016-08-31 12:34:54 UTC
Permalink
Post by Robert Kern
Post by Matti Viljamaa
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].
Post by Robert Kern
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Robert Kern
2016-08-31 12:42:07 UTC
Permalink
Post by Robert Kern
Post by Matti Viljamaa
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].
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

Loading...