【问题标题】:Fastest way to remove same indices from each row in 2D array从二维数组中的每一行删除相同索引的最快方法
【发布时间】:2022-08-12 18:56:38
【问题描述】:

我正在寻找最快的方法(最好使用 numpy)来删除 2D 数组的每一行中的索引列表。举个例子:

matrix = [[1,2,3,4,5],  
           [4,5,6,7,8],
           [7,8,9,10,11]]
indices_to_delete = [2,3]

现在的目标是从每一行中删除这些索引,以获得:

result = [[1,2,5],  
         [4,5,8],
         [7,8,11]]

我目前的方法是使用以下方法对每一行分别执行此操作:

result = []
for row in array:
    result.append(np.delete(row, indices_to_delete))

有没有更快的方法来做到这一点?

    标签: python numpy


    【解决方案1】:

    您可以沿不同的轴使用.delete

    >>> np.delete(matrix, indices_to_delete, axis=1)
    array([[ 1,  2,  5],
           [ 4,  5,  8],
           [ 7,  8, 11]])
    

    【讨论】:

      猜你喜欢
      • 2015-04-08
      • 2017-10-29
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      • 2020-10-25
      相关资源
      最近更新 更多