【发布时间】:2021-03-26 05:16:14
【问题描述】:
我正在尝试使用条件从多维 numpy 数组中删除子数组。在这个例子中,我想删除所有包含值 999 的子数组。这是我失败的尝试之一:
a = np.array([[[1,2,3], [1,2,3]],
[[999,5,6], [4,5,6]],
[[999,8,9], [7,999,9]]
])
for i in range(0,len(a)):
if 999 in a[i]:
np.delete(a, i, 0)
我想要的结果是:
array([[1,2,3], [1,2,3]])
这只是一个小例子,它应该可以帮助我理解一个更大的问题,它看起来像这样:
# win_list_hyper.shape -> (1449168, 233)
# win_list_multi.shape -> (1449168, 12, 5, 5)
win_list_hyper = np.where(win_list_hyper <= 0, -3.40282e+38, win_list_hyper)
win_list_multi = np.where(win_list_multi <= 0, -3.40282e+38, win_list_multi)
# fail!:
for i in range(0,len(win_list_multi)):
if -3.40282e+38 in win_list_multi[i] or -3.40282e+38 in win_list_hyper[i]:
np.delete(win_list_multi, i, 0)
np.delete(win_list_hyper, i, 0)
(顺便说一句。如果您知道如何提高效率,请告诉我!)
【问题讨论】:
标签: python arrays numpy for-loop conditional-statements