测试numpy数组中的每个元素是否位于两个值之间的简便方法? [英] Easy way to test if each element in an numpy array lies between two values?
问题描述
我想知道是否有一种语法上简单的方法来检查numpy数组中的每个元素是否位于两个数字之间.
I was wondering if there was a syntactically simple way of checking if each element in a numpy array lies between two numbers.
换句话说,就像numpy.array([1,2,3,4,5]) < 5
将返回array([True, True, True, True, False])
一样,我想知道是否可以做与此类似的事情:
In other words, just as numpy.array([1,2,3,4,5]) < 5
will return array([True, True, True, True, False])
, I was wondering if it was possible to do something akin to this:
1 < numpy.array([1,2,3,4,5]) < 5
...获得...
array([False, True, True, True, False])
我知道我可以通过布尔测试的逻辑链接来获得此信息,但是我正在研究一些相当复杂的代码,我一直在寻找语法上干净的解决方案.
I understand that I can obtain this through logical chaining of boolean tests, but I'm working through some rather complex code and I was looking for a syntactically clean solution.
有什么提示吗?
推荐答案
一种解决方案是:
a = numpy.array([1,2,3,4,5])
(a > 1).all() and (a < 5).all()
如果要使用真值的摘要数组,请使用:
if you want the acutal array of truth vaues, just use:
(a > 1) & (a < 5)
这篇关于测试numpy数组中的每个元素是否位于两个值之间的简便方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!