[Numpy-discussion] Backwards slicing including the first element
Warren Weckesser
warren.weckesser@enthought....
Wed Mar 10 09:19:25 CST 2010
Warren Weckesser wrote:
> Jerome Esteve wrote:
>
>> 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 ?
>>
>
> You could use a[i:i-(len(a)+k):-1]. This works because a[-len(a)] is
> the same as a[0].
>
>
I'm going to be pedantic and amend that last sentence. While it is true
the a[-len(a)] is the same as a[0], that is not why this works. A
better explanation is that Python is lenient about handling the value
given as the end position in a slice. It does not have to be a valid
index. If the value is out of range, Python will include everything up
to the end of the actual data, and it will not raise an error. So you
can do slices like the following:
-----
In [101]: w
Out[101]: [10, 11, 12, 13, 14]
In [102]: w[2:-5:-1]
Out[102]: [12, 11]
In [103]: w[2:-6:-1]
Out[103]: [12, 11, 10]
In [104]: w[2:-7:-1]
Out[104]: [12, 11, 10]
-----
Note that -6 and -7 are not valid indices; using w[-6] will raise an
IndexError.
Warren
> For example:
>
> -----
> In [57]: a
> Out[57]: array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
>
> In [58]: i = 3
>
> In [59]: for k in range(5):
> ....: print k, a[i:i-(len(a)+k):-1]
> ....:
> ....:
> 0 []
> 1 [13]
> 2 [13 12]
> 3 [13 12 11]
> 4 [13 12 11 10]
>
> -----
>
>
> Warren
>
>
>> Many thanks in advance, Jerome.
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
More information about the NumPy-Discussion
mailing list