4

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?

0

3 Answers 3

10

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

Sign up to request clarification or add additional context in comments.

Comments

2

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]

2 Comments

You can also use an itertools.groupby to do both in one shot, but then the type conversion gets a bit messier
It was a little unclear, but the OP is definitely using numpy, so this definitely shouldn't be used
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.