Dzung Nguyen
Dzung Nguyen

Reputation: 3942

Get a subarray from a numpy array based on index

I have a numpy array vector, and I want to get a subset based on the indexes:

import numpy as np
input=np.array([1,2,3,4,5,6,7,8,9,10])
index=np.array([0,1,0,0,0,0,1,0,0,1])

what is a pythonic way to get out output=[2,7,10]?

Upvotes: 3

Views: 4803

Answers (1)

Fred Foo
Fred Foo

Reputation: 363838

output = input[index.astype(np.bool)]

or

output = input[np.where(index)[0]]

Upvotes: 8

Related Questions