Numpy链与两个谓词的比较 [英] Numpy chain comparison with two predicates
本文介绍了Numpy链与两个谓词的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Numpy中,我可以生成一个布尔数组,如下所示:
In Numpy, I can generate a boolean array like this:
>>> arr = np.array([1, 2, 1, 2, 3, 6, 9])
>>> arr > 2
array([False, False, False, False, True, True, True], dtype=bool)
是否可以将比较链接在一起?例如:
Is is possible to chain comparisons together? For example:
>>> 6 > arr > 2
array([False, False, False, False, True, False, False], dtype=bool)
推荐答案
最接近的AFAIK是使用&
,|
和^
:
AFAIK the closest you can get is to use &
, |
, and ^
:
>>> arr = np.array([1, 2, 1, 2, 3, 6, 9])
>>> (2 < arr) & (arr < 6)
array([False, False, False, False, True, False, False], dtype=bool)
>>> (2 < arr) | (arr < 6)
array([ True, True, True, True, True, True, True], dtype=bool)
>>> (2 < arr) ^ (arr < 6)
array([ True, True, True, True, False, True, True], dtype=bool)
我认为您无法使用a < b < c
样式的链接来工作.
I don't think you'll be able to get a < b < c
-style chaining to work.
这篇关于Numpy链与两个谓词的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文