3

Say we have two arrays

a = np.array([1,2,3,4]).reshape(2,2)
b = np.array([True, False, False, True]).reshape(2,2)

gives

a = [[1, 2],
     [3, 4]] 
b = [[True, False],
     [False, True]]

We can do a[b] to get only the values of b that are true giving us [1, 4]

I tried to do a[not b] to get those that are false but got an error. I know I can do a[b == False] but want to do it in a Pythonic way.

Any solution?

1
  • 9
    a[~b] might be what you are looking for Commented Sep 19, 2020 at 19:47

1 Answer 1

1

You can use either np.logical_not or the ~ operator:

>>> a[np.logical_not(b)]
>>> a[~b]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.