Home > Blockchain >  Return the indices of "false" values in a boolean array
Return the indices of "false" values in a boolean array

Time:11-23

I feel like this is a really simple question but I can't find the solution.

Given a boolean array of true/false values, I need the output of all the indices with the value "false". I have a way to do this for true:

test = [ True False True True]

test1 = np.where(test)[0]

This returns [0,2,3], in other words the corresponding index for each true value. Now I need to just get the same thing for the false, where the output would be [1]. Anyone know how?

CodePudding user response:

Use np.where(~test) instead of np.where(test).

CodePudding user response:

With enumerate:

>>> test = [True, False, True, True]
>>> [i for i, b in enumerate(test) if b]
[0, 2, 3]
>>> [i for i, b in enumerate(test) if not b]
[1]

CodePudding user response:

Using explicite np.array().

import numpy as np
test = [True, False, True, True]
a = np.array(test)
test1 = np.where(a==False)[0]    #np.where(test)[0]
print(test1)
  • Related