I have a numpy array, called a
, I want to check whether it contains an item in a range, specified by two values.
import numpy as np
a = np.arange(1
Adding to the pure Numpy answer we can also use itertools
import itertools
bool(list(itertools.ifilter(lambda x: 33 <= x <= 66, a)))
For smaller arrays this would suffice:
bool(filter(lambda x: 33 <= x <= 66, a))
Numpy arrays doesn't work well with pythonic a < x < b
. But there's func for this:
np.logical_and(a > mintrshold, a < maxtreshold)
or
np.logical_and(a > mintrshold, a < maxtreshold).any()
in your particular case. Basically, you should combine two element-wise ops. Look for logic funcs for more details