Numpy 链与两个谓词的比较 [英] Numpy chain comparison with two predicates

查看:35
本文介绍了Numpy 链与两个谓词的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Numpy 中,我可以生成这样的布尔数组:

<预><代码>>>>arr = np.array([1, 2, 1, 2, 3, 6, 9])>>>>2数组([假,假,假,假,真,真,真],dtype=bool)

是否可以将比较链接在一起?例如:

<预><代码>>>>6 >>2数组([假,假,假,假,真,假,假],dtype=bool)

解决方案

AFAIK 最接近的是使用 &|^:

<预><代码>>>>arr = np.array([1, 2, 1, 2, 3, 6, 9])>>>(2 < arr) &(arr < 6)数组([假,假,假,假,真,假,假],dtype=bool)>>>(2 < arr) |(arr < 6)数组([真,真,真,真,真,真,真],dtype=bool)>>>(2 < arr) ^ (arr < 6)数组([真,真,真,真,假,真,真],dtype=bool)

我不认为你能得到 a <<c 风格的链接工作.

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 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)

I don't think you'll be able to get a < b < c-style chaining to work.

这篇关于Numpy 链与两个谓词的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆