1

I'm curious about something on boolean indexing:

I know that you can operate with two [gt/lt] conditions, as in

 in[1]: import numpy as np
 in[2]: x = np.arrange(7)
 in[3]: (x > 3) & (x < 6)
out[3]: array([False, False, False, False, True, True, False])

But I wonder if there is a way I can do the same with two "equals to" conditions, without resorting to a for loop, something like:

 in[4]: x = np.arrange(7)
 in[5]: (x == 3) & (x == 5)
out[5]: array([False, False, False, True, False, True, False]) #expected result#

(Learning python, sorry for wrong terms)

yjtpesesu
  • 11
  • 1

1 Answers1

0

You just need or instead of and like so:

x = np.arrange(7)
(x == 3) | (x == 5)
>>> array([False, False, False,  True, False,  True, False])
user15270287
  • 1,123
  • 4
  • 11
  • Thanks, man. I actually tried with an "or", but it didn't work. What's exactly the difference? – yjtpesesu Mar 09 '21 at 01:53
  • [this](https://stackoverflow.com/questions/22646463/and-boolean-vs-bitwise-why-difference-in-behavior-with-lists-vs-nump) should help explain – user15270287 Mar 09 '21 at 04:49