daiyue
daiyue

Reputation: 7448

find the index of a boolean array whose values are true

I am wondering whats the best way to find all the indices of a Boolean array, of which the values are True. For example, an array of bool values,

import numpy as np
A = np.array([True, False, True, False, True])
true_list = A[A == True].index.tolist()

Upvotes: 7

Views: 31577

Answers (1)

John Zwinck
John Zwinck

Reputation: 249093

This will do it easily:

np.where(A)

Or:

A.nonzero()

Upvotes: 24

Related Questions