根据范围内的值过滤numpy数组中的行 [英] filtering lines in a numpy array according to values in a range
本文介绍了根据范围内的值过滤numpy数组中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让一个数组:
a =np.array([[1,2],[3,-5],[6,-15],[10,7]])
要获得第二列元素在-6以上的行,
to get lines with elements of the second column above -6 it' s possible to do
>>> a[a[:,1]>-6]
array([[ 1, 2],
[ 3, -5],
[10, 7]])
但是如何获取第二个元素在-6; 3之间的行呢? 我试过了:
but how to get lines with the second element between -6;3? I tried:
>>> a[3>a[:,1]>-6]
以及(会引发错误):
>>> np.ma.masked_inside(a,-6,3)
给出:
masked_array(data =
[[-- --]
[-- --]
[6 -15]
[10 7]],
mask =
[[ True True]
[ True True]
[False False]
[False False]],
fill_value = 999999)
但结果不太清楚
谢谢 jp
推荐答案
>>> a[ (3>a[:,1]) & (a[:,1]>-6) ]
array([[ 1, 2],
[ 3, -5]])
这篇关于根据范围内的值过滤numpy数组中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文