如何使用一对不等式过滤 numpy 数组,例如:
>>> 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()
如果我尝试,我会得到相同的响应a.all(3 < a <= 6)
np.array([x for x in a if 3 < x <= 6])
有效,但看起来很讨厌。这样做的正确方法是什么?