[Numpy-discussion] why does b[:-0] not work, and is there an elegant solution?
Neil Martinsen-Burrell
nmb@wartburg....
Wed Aug 19 07:50:46 CDT 2009
On Aug 19, 2009, at 7:25 AM, Mark Bakker wrote:
> I compute the index of the last term in an array that I need and
> call the index n.
>
> I can then call the array b as
>
> b[:-n]
>
> If I need all terms in the array, the logical syntax would be:
>
> b[:-0]
>
> but that doesn't work. Any reason why that has not been implemented?
> Any elegant workaround?
Because there is no negative zero as an integer:
>>> -0 == 0
True
So when Python parses your request, it sees "-0" and replaces that
with the integer 0. And as you found out, b[:0] gives you an empty
slice. Negative indices are just syntactic sugar for (N+1)-n where N
is the length of the list and that should work for n=0 as well:
>>> b = [1,2,3,4,5]
>>> b[:0]
[]
>>> b[:len(b)+1-0]
[1, 2, 3, 4, 5]
-Neil
More information about the NumPy-Discussion
mailing list