[Numpy-discussion] Backwards slicing including the first element

Jerome Esteve esteve@lkb.ens...
Wed Mar 10 07:33:55 CST 2010


Dear all,

Is there a way to give an integer value to j when using a[i:j:-1] so that the first element of the array can be included in the slice ?

I would like to use some code like a[i:i-k:-1] to get a slice of length k. 

The numpy documentation seems to suggest that j=-1 should work:

"Assume n is the number of elements in the dimension being sliced. Then, if i is not given it defaults to 0 for k > 0 and n for k < 0 . If j is not given it defaults to n for k > 0 and -1 for k < 0 . If k is not given it defaults to 1."

But a[i:i-k:-1] is empty if i-k is -1. The workaround is a[i::-1][:k], is there something simpler ?

Many thanks in advance, Jerome.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100310/2c89b5e3/attachment.html 


More information about the NumPy-Discussion mailing list