假设我有以下数组,我只想保留第一列值为 39 (±1)、第二列值为 -1 (±1) 的行:
[[39.26783752, -0.84259111],
[24.44051933, 2.33262944],
[28.28138924, 6.79832602],
[37.54968262, 7.43038321]]
我知道如果我有更简单的条件,我可以这样做:
np.where((next_points_arr[:, 0] > 39) & (next_points_arr[:, 1] < 1))
我也知道我可以使用 math.isclose(39, 39.2, abs_tol = 1)
,但我不确定如何有效地将两者结合起来?
我的方法是再创建两列带有 bool 值的列,这将表示条件是否为真,然后使用 np.where()
查找 True
在每一列中,但这似乎并不明智。
最佳答案
您可以使用 np.isclose
检查这两个条件
a = np.array(
[[39.26783752, -0.84259111],
[24.44051933, 2.33262944],
[28.28138924, 6.79832602],
[37.54968262, 7.43038321]])
a[np.isclose(a, [39, -1], atol=[1.,1.]).all(1)]
输出:
array([[39.26783752, -0.84259111]])
关于python - 如何根据模糊条件从Numpy数组中选择值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64445525/