I have two NumPy arrays, e.g.:
a = [1,2,3,4,5]
and a filter array, e.g.:
f = [False, True, False, False, True]
len(a) == l
If you don't already need numpy arrays, here's with a plain list:
import itertools
print itertools.compress(a, f)
For pre-2.7 versions of python, you must roll your own (see manual):
def compress(data, selectors):
return (d for d, s in itertools.izip(data, selectors) if s)
NumPy supports boolean indexing
a[f]
This assumes that a
and f
are NumPy arrays rather than Python lists (as in the question). You can convert with f = np.array(f)
.