How can I filter a numpy array using a pair of inequalities, such as:
>>> a = np.arange(10)
>>> a[a <= 6]
array([0, 1, 2, 3, 4, 5, 6])
>>> a[3 < a]
array([4, 5, 6, 7, 8, 9])
>>>
>>> a[3 < a <= 6]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous.
Use a.any() or a.all()
I get the same response if I try a.all(3 < a <= 6)
np.array([x for x in a if 3 < x <= 6])
works, but it seems nasty. What's the right way to do this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…