5

With some numpy array a, what I'd like to do is

indices = np.where((a < 4) or (a > 12))

This isn't valid. It just returns "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()". But this expression isn't ambiguous, and any and all don't do what I want to do. (any and all can't take compound expressions either. But if can. Confused...)

5
  • 4
    indices = np.where((a < 4) | (a > 12)) Aside: this is a duplicate. Commented Mar 12, 2013 at 22:51
  • Sorry for the duplication; I couldn't find any related article. Apparently because this has nothing to do with np.where(), but rather the fact that Python has <i>both</i> or and | and they do different things. Oh, Python, you silly beast... Commented Mar 12, 2013 at 22:58
  • No problem. It is oft-times difficult to find duplicates. Those two operators do different things. Commented Mar 12, 2013 at 23:00
  • They are not the same operator... Commented Mar 12, 2013 at 23:00
  • Yes, hence my comment about them doing different things. Does Python finish the craziness and have both and and & too? EDIT: Yes, yes, it does. sigh Commented Mar 12, 2013 at 23:06

1 Answer 1

12

You want to get a logical/boolean array as your argument for where

You can do x | y or np.logical_or(x,y) , where x and y are a < 4 and a > 12

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.