Michel Keijzers
Michel Keijzers

Reputation: 15367

Convert numpy array from values to indices

I have a numpy array a: [True, False, True, False, False, ...] And I like to have a numpy array that has the indices of the True and False values, i.e. [0, 2, ...] and [1, 3, 4, ...]

Upvotes: 2

Views: 2582

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 602575

To get the indices of the True values in a, you can use

a.nonzero()

For the indices of the False values, use

(~a).nonzero()

Upvotes: 9

Related Questions