This question already has answers here:
Extract elements from numpy array, that are not in list of indexes

(4个答案)


在10个月前关闭。




说我有一些长数组和索引列表。我该如何选择除那些索引以外的所有内容?我找到了一个解决方案,但它并不优雅:
import numpy as np
x = np.array([0,10,20,30,40,50,60])
exclude = [1, 3, 5]
print x[list(set(range(len(x))) - set(exclude))]

最佳答案

这就是 numpy.delete 所做的。 (它不会修改输入数组,因此您不必担心。)

In [4]: np.delete(x, exclude)
Out[4]: array([ 0, 20, 40, 60])

10-06 10:32
查看更多