按索引条件过滤数组

时间:2019-10-01 15:00:43

标签: python arrays numpy

我需要按索引条件过滤数组。 numpy中有如下功能:

np.where_index(lambda indices: indices[0]**2 + indices[1]**2 < 10, a)

(类似于按值np.where(a > 2, a)进行过滤)

1 个答案:

答案 0 :(得分:0)

您可以像下面这样使用遮罩

arr = np.arange(25).reshape(5,5)
display(arr)
ind_x = np.arange(arr.shape[0]).reshape(-1,1)
ind_y = np.arange(arr.shape[1])

ind = (ind_x**2 + ind_y**2)<10

arr[ind]
相关问题