naz
naz

Reputation: 2072

Multiple ranges of numpy array returned

Suppose there is an array like the below:

a = np.array([[1,2],
              [2,3],
              [2,3],
              [2,3],
              [4,5],
              [3,4],
              [2,3]])

How would I return multiple rows, this is what I would like to achieve (I know the syntax is wrong, but this will give you an idea of what I want to achieve):

a[0:2 & 5:6,:]

I would not be able to pass individual rows because in my actual code I would need to include larger ranges, e.g. 20:60 & 90:160, etc.

Upvotes: 2

Views: 1783

Answers (1)

Mike Müller
Mike Müller

Reputation: 85432

This works:

>>> a[np.r_[:2, 5:6], :]
array([[1, 2],
       [2, 3],
       [3, 4]])

The np.r_:

Translates slice objects to concatenation along the first axis.

Upvotes: 3

Related Questions