Getting only those values that fulfill a condition in a numpy array
arrays, numpy, python
Solution
How about:
In [19]: b = a[s]
In [20]: b[b > 0]
Out[20]: array([2, 3, 4])
Problem
There must a be a (very) quick and efficient way to get only elements from a numpy array, or even more interestingly from a slice of it. Suppose I have a numpy array: ``` import numpy as np a = np.arange(-10,10) ``` Now if I have a list: ``` s = [9, 12, 13, 14] ``` I can select elements from a: ``` a[s] #array([-1, 2, 3, 4]) ``` How can I have an (numpy) array made of the elements from a[s] that fulfill a condition, i.e. are positive (or negative)? It should result ``` np.ifcondition(a[s]>0, a[s]) #array([2, 3, 4]) ``` It looks trivial but I was not able to find a simple and condensed expression. I'm sure masks do but it's doesn't look really direct to me. However, neither: ``` a[a[s]>0] a[s[a[s]>0]] ``` are in fact good choices.