I was wondering if there was a syntactically simple way of checking if each element in a numpy array lies between two numbers.
我想知道是否有一种语法上简单的方法来检查numpy数组中的每个元素是否位于两个数字之间。
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:
换句话说,正如numpy.array([1,2,3,4,5])<5将返回数组([True,True,True,True,False]),我想知道是否有可能做类似于此的东西:
1 < numpy.array([1,2,3,4,5]) < 5
1
... to obtain ...
......获得......
array([False, True, True, True, False])
数组([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.
我知道我可以通过布尔测试的逻辑链接来获得这个,但我正在研究一些相当复杂的代码,我正在寻找一个语法上干净的解决方案。
Any tips?
1 个解决方案
#1
44
one solution would be:
一个解决方案是:
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:
如果你想要真正的vaues的真实数组,只需使用:
(a > 1) & (a < 5)
#1
44
one solution would be:
一个解决方案是:
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:
如果你想要真正的vaues的真实数组,只需使用:
(a > 1) & (a < 5)