0

Say I have a np.array: my_array = [ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]

So to check a single equals condition I can do my_array == 1 and get an array of booleans.

Is there a way to extend this to check for multiple condition at once, something like my_array == [1,2] (which doesn't work). I want to avoid using AND as I'll be adding many such conditions which will change dynamically in a loop.

Is there an overall better way to approach this, maybe np.where?

To clarify: I wanted to find all elements in my_array for more than one equals condition at the same time.

N Blake
  • 141
  • 4
  • 5
    " something like my_array == [1,2] (which doesn't work)" What did you want the result to be? What is the actual rule you are trying to implement? Obviously, none of the values in your array can be equal to the list `[1, 2]`, and also none of them can simultaneously be equal to 1 and equal to 2. Did you want to find values that are equal to *any of* the values in `[1, 2]`? Did you want to do *separate tests for each* and accumulate the results somehow? Something else? – Karl Knechtel Jan 21 '22 at 13:26
  • 2
    What's the problem with using AND for multiple conditions? – MisterMiyagi Jan 21 '22 at 13:27
  • 1
    a decent example would be nice here – Eumel Jan 21 '22 at 13:28
  • 2
    Does https://stackoverflow.com/questions/15939748/check-if-each-element-in-a-numpy-array-is-in-another-array answer your question? – Karl Knechtel Jan 21 '22 at 13:31
  • Thanks Karl that worked perfectly. I was looking for any of the values in [1, 2]. – N Blake Jan 21 '22 at 13:46
  • Should I clarify the question since it wasn't clear or delete this as its stated elsewhere? – N Blake Jan 21 '22 at 13:53
  • Please [edit] the question to clarify it if you can. If the other Q&A answers your question, you can accept it as a duplicate (which I just did for you, following your comment). – MisterMiyagi Jan 21 '22 at 13:54

1 Answers1

-1

I suppose it depends what you're trying to return, but you could try using np.where, for example:

array = np.array([1,2,3,4,5,6,7,8,9,10])
sifted = np.where(array % 2)

Will return a numpy array of odd numbers.

Python's built in all() could be useful to you as well, depending on what you are doing with the output. The all() functions performs a conjunction across all members of a list on a given condition i.e.:

all(x % 2 for x in array)

This would return false in the above case.

Finally another useful thing to consider here are lambda functions and list comprehensions... By the sounds of it you are trying to do something flexible and dynamic, hence wanting the bools. You can use list comprehension to get the items you want directly, but this can get labour intensive if you're doing a lot of different things:

new_array = [x for x in array if x % 2]

A perhaps better alternative may be to specify lambda functions as and when you need them:

lam = lambda x : x % 2
print(lam(array))

This will return an array of the kind :

[1 0 1 0 1 0 1 0 1 0]

You can combine this with a filter to filter your results:

lam = lambda x : x % 2
filtered = list(filter(lam, array))
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • you are checking if the element modulo 2 is not 0 which is not what the question asked. Question was to check if the elements are 1 or 2. –  Jan 21 '22 at 14:20
  • They're asking how to check multiple members of a list without using AND. They gave 1,2 as a trivial example. – AlbertDoesProgramming Jan 21 '22 at 14:29
  • 1
    Yes but I don't see how using the modulo operator solves the problem. In your case your resulting array is true for odd numbers and false for even numbers. How does this solve a problem where is the number is 1 or 2 returns true otherwise returns false? –  Jan 21 '22 at 15:29
  • You're focussing on the modulo operator, but the answer is suggesting different ways of testing conditions across numpy arrays. Using lambda and filters, all() and np.where(). The condiition itself is insigificant. You are misreading OPs question. – AlbertDoesProgramming Jan 21 '22 at 20:46
  • It looks like all the ways of testing conditions still just test one condition. The goal of the question is to "extend this to check for multiple condition at once". – MisterMiyagi Jan 21 '22 at 20:51