17

Numpy tentative tutorial suggests that a[ : :-1] is a reversed a. Can someone explain me how we got there?

I understand that a[:] means for each element of a (with axis=0). Next : should denote the number of elements to skip (or period) from my understanding.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Denys S.
  • 6,358
  • 12
  • 43
  • 65
  • 5
    See http://stackoverflow.com/questions/509211/the-python-slice-notation -- This is for python proper, but numpy obeys all of the conventions like a good 3rd party package. – mgilson Apr 01 '13 at 17:17
  • 2
    Also see http://stackoverflow.com/a/4371049/875127 for a good explanation of how basic slicing in numpy returns a view and not a copy of the original array. – Cianan Sims Apr 01 '13 at 17:21

4 Answers4

20

It isn't numpy, it's Python.

In Python, there are slices for sequence/iterable, which come in the following syntax

seq[start:stop:step] => a slice from start to stop, stepping step each time.

All the arguments are optional, but a : has to be there for Python to recognize this as a slice.

Negative values, for step, also work to make a copy of the same sequence/iterable in reverse order:

>>> L = range(10)
>>> L[::-1] 
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

And numpy follows that "rule" like any good 3rd party library..

>>> a = numpy.array(range(10))
>>> a[::-1]
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

See this link

pradyunsg
  • 18,287
  • 11
  • 43
  • 96
10

As others have noted, this is a python slicing technique, and numpy just follows suit. Hopefully this helps explain how it works:

The last bit is the stepsize. The 1 indicates to step by one element at a time, the - does that in reverse.

Blanks indicate the first and last, unless you have a negative stepsize, in which case they indicate last and first:

In [1]: import numpy as np

In [2]: a = np.arange(5)

In [3]: a
Out[3]: array([0, 1, 2, 3, 4])

In [4]: a[0:5:1]
Out[4]: array([0, 1, 2, 3, 4])

In [5]: a[0:5:-1]
Out[5]: array([], dtype=int64)

In [6]: a[5:0:-1]
Out[6]: array([4, 3, 2, 1])

In [7]: a[::-2]
Out[7]: array([4, 2, 0])

Line 5 gives an empty array since it tries to step backwards from the 0th element to the 5th.
The slice doesn't include the 'endpoint' (named last element) so line 6 misses 0 when going backwards.

askewchan
  • 45,161
  • 17
  • 118
  • 134
  • Why does `a[5:-1:-1]` return `array([], dtype=int64)` and not the right answer `array([4, 3, 2, 1, 0])`? – anaik Nov 18 '18 at 03:32
  • 4
    @AbhishekNaik In python indexing, [negative numbers count from the end toward](https://docs.python.org/3/tutorial/introduction.html#lists) the beginning, so `a[-1]` is the last element. Thus, `a[5:-1:-1]` is equivalent to `a[5:4:-1]`, so the empty array *is* the right answer. To get to the end (in either direction) use `None` or leave it blank: `a[5:None:-1]` or `a[5::-1]`. – askewchan Nov 22 '18 at 01:58
5

This isn't specific to numpy, the slice a[::-1] is equivalent to slice(None, None, -1), where the first argument is the start index, the second argument is the end index, and the third argument is the step. None for start or stop will have the same behavior as using the beginning or end of the sequence, and -1 for step will iterate over the sequence in reverse order.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
0

You can use the reversed Python built-in:

import numpy as np

bins = np.arange(0.0, 1.1, .1)
for i in reversed(bins):
    print(i)
Aaron Lelevier
  • 19,850
  • 11
  • 76
  • 111