[英]Fastest way to remove same indices from each row in 2D array
I am looking for the fastest way (preferably with numpy) to delete a list of indices in each row of a 2D array.我正在寻找最快的方法(最好使用 numpy)来删除 2D 数组的每一行中的索引列表。 As an example:
举个例子:
matrix = [[1,2,3,4,5],
[4,5,6,7,8],
[7,8,9,10,11]]
indices_to_delete = [2,3]
And now the goal is to delete these indices form each row, to get:现在的目标是从每一行中删除这些索引,以获得:
result = [[1,2,5],
[4,5,8],
[7,8,11]]
My current approach would be to do this separately for each row using:我目前的方法是使用以下方法对每一行分别执行此操作:
result = []
for row in array:
result.append(np.delete(row, indices_to_delete))
Is there a faster way of doing this?有没有更快的方法来做到这一点?
You can use .delete
along different axis:您可以使用
.delete
沿不同的轴:
>>> np.delete(matrix, indices_to_delete, axis=1)
array([[ 1, 2, 5],
[ 4, 5, 8],
[ 7, 8, 11]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.