43

I'd like to select multiple, non-adjacent ranges from a 1d numpy array (or vector).

Suppose:

>>> idx = np.random.randint(100, size=10)
array([82,  9, 11, 94, 31, 87, 43, 77, 49, 50])

This works, of course:

>>> idx[0:3]
array([82,  9, 11])

And this works to fetch via individual indices:

>>> idx[[0,3,4]]
array([82, 94, 31])

But what if I want to select the ranges 0:3, and 7:?

I've tried:

>>> idx[[0:3,7:]]
SyntaxError: invalid syntax

Is there a simple way to do this, or do I need to generate them separately and concatenate?

alexw
  • 8,468
  • 6
  • 54
  • 86

1 Answers1

64

You need to concatenate, either before or after indexing. np.r_ makes it easy

In [116]: idx=np.array([82,  9, 11, 94, 31, 87, 43, 77, 49, 50])
In [117]: np.r_[0:3,7:10]
Out[117]: array([0, 1, 2, 7, 8, 9])
In [118]: idx[np.r_[0:3,7:10]]
Out[118]: array([82,  9, 11, 77, 49, 50])

np.r_ expands the slices and concatenates them.

You can mix slices and lists:

In [120]: np.r_[0:3,7:10,[0,3,4]]
Out[120]: array([0, 1, 2, 7, 8, 9, 0, 3, 4])

Concatenating before indexing is probably faster than after, but for 1d array like this, I don't think the difference is significant.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • 1
    Note: I tried using np.r_ without fully specifying indexes, i.e. np.r_[:3,7:] and the result was very strange. Using it by fully specifiying start and end indexes (np.r_[0:3,7:10]) worked as expected. – BlueCoder Nov 22 '19 at 15:41
  • 1
    @BlueCoder, In normal indexing, the `stop` value for `[7:]` can be deduced from the array/list that's indexed. `r_` can't do that. It looks like it treats the `7` as a `stop` rather than `start`. An alternative would have been to raise an error. Examine the `r_` code for details. It's best to provide both `start` and `stop` when using `r_`. – hpaulj Nov 22 '19 at 17:04
  • 1
    @hpaulj And how would that work for a 2d array? Let 's say I want different non-adjacent row ranges specific to each column. How would that work? – xicocaio Aug 12 '20 at 14:21
  • @CodeNoob I asked a question about the case of the 2D array with non-adjacent row ranges and received a [pretty good answer here](https://stackoverflow.com/q/63380108/2464671). – xicocaio Dec 15 '20 at 11:15