三行代码  ›  专栏  ›  技术社区  ›  user18790522

在Python中搜索特定行并从数组中删除

  •  0
  • user18790522  · 技术社区  · 1 月前

    我有一个数组 A . 我想寻找全为零的行,然后删除它们。这里我要删除 A[:,2] 并将行索引附加到 B . 所需输出已连接。

    import numpy as np
    
    A=np.array([[ 0.00000000e+00,  3.57765318e-08,  0.00000000e+00,
             1.74215085e-07,  0.00000000e+00,  0.00000000e+00,
             0.00000000e+00],
           [-1.06733099e-07,  0.00000000e+00,  0.00000000e+00,
             0.00000000e+00,  3.50573100e-08,  0.00000000e+00,
             0.00000000e+00],
           [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
             0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
             0.00000000e+00],
           [-1.78530448e-07,  0.00000000e+00,  0.00000000e+00,
             0.00000000e+00,  1.61650808e-08,  9.06895783e-08,
             0.00000000e+00],
           [ 0.00000000e+00, -4.46583743e-08,  0.00000000e+00,
            -5.99482549e-08,  0.00000000e+00,  0.00000000e+00,
             1.04606629e-07],
           [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
            -9.64183682e-08,  0.00000000e+00,  0.00000000e+00,
             9.64183682e-08],
           [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
             0.00000000e+00, -1.25633650e-07, -4.03926936e-07,
             0.00000000e+00]])
    
    B=[5,8]
    

    所需输出为

    array([[ 0.00000000e+00,  3.57765318e-08,  0.00000000e+00,
             1.74215085e-07,  0.00000000e+00,  0.00000000e+00,
             0.00000000e+00],
           [-1.06733099e-07,  0.00000000e+00,  0.00000000e+00,
             0.00000000e+00,  3.50573100e-08,  0.00000000e+00,
             0.00000000e+00],
           [-1.78530448e-07,  0.00000000e+00,  0.00000000e+00,
             0.00000000e+00,  1.61650808e-08,  9.06895783e-08,
             0.00000000e+00],
           [ 0.00000000e+00, -4.46583743e-08,  0.00000000e+00,
            -5.99482549e-08,  0.00000000e+00,  0.00000000e+00,
             1.04606629e-07],
           [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
            -9.64183682e-08,  0.00000000e+00,  0.00000000e+00,
             9.64183682e-08],
           [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
             0.00000000e+00, -1.25633650e-07, -4.03926936e-07,
             0.00000000e+00]])
    B=[2,5,8]
    
    4 回复  |  直到 1 月前
        1
  •  1
  •   mozway    1 月前

    您可以使用:

    # find all rows where all values are 0
    x = np.where((A==0).all(1))[0]
    
    # delete them from A
    A = np.delete(A, x, axis=0)
    
    # prepend them in B
    B = np.r_[x, B]
    

    备选方案:

    mask = (A==0).all(1)
    x = np.where(mask)[0]
    
    A = A[~mask]
    
    B = np.r_[x, B]
    

    输出:

    # A
    array([[ 0.00000000e+00,  3.57765318e-08,  0.00000000e+00,
             1.74215085e-07,  0.00000000e+00,  0.00000000e+00,
             0.00000000e+00],
           [-1.06733099e-07,  0.00000000e+00,  0.00000000e+00,
             0.00000000e+00,  3.50573100e-08,  0.00000000e+00,
             0.00000000e+00],
           [-1.78530448e-07,  0.00000000e+00,  0.00000000e+00,
             0.00000000e+00,  1.61650808e-08,  9.06895783e-08,
             0.00000000e+00],
           [ 0.00000000e+00, -4.46583743e-08,  0.00000000e+00,
            -5.99482549e-08,  0.00000000e+00,  0.00000000e+00,
             1.04606629e-07],
           [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
            -9.64183682e-08,  0.00000000e+00,  0.00000000e+00,
             9.64183682e-08],
           [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
             0.00000000e+00, -1.25633650e-07, -4.03926936e-07,
             0.00000000e+00]])
    
    # B
    array([2, 5, 8])
    
        2
  •  0
  •   fsimonjetz    1 月前

    IIUC,你可以这样做:

    # the index of the first matching row
    x = np.all(A==0, axis=1).argmax()
    
    A_bar = np.delete(A, x, axis=0)
    B.append(x)
    
        3
  •  0
  •   constantstranger    1 月前

    有一种方法:

    C = np.all(A==0, axis=1)
    D = np.arange(A.shape[0])[C]
    A = A[~C, :]
    print(A)
    B.extend(D)
    print(B)
    

    输出:

    [[ 0.00000000e+00  3.57765318e-08  0.00000000e+00  1.74215085e-07
       0.00000000e+00  0.00000000e+00  0.00000000e+00]
     [-1.06733099e-07  0.00000000e+00  0.00000000e+00  0.00000000e+00
       3.50573100e-08  0.00000000e+00  0.00000000e+00]
     [-1.78530448e-07  0.00000000e+00  0.00000000e+00  0.00000000e+00
       1.61650808e-08  9.06895783e-08  0.00000000e+00]
     [ 0.00000000e+00 -4.46583743e-08  0.00000000e+00 -5.99482549e-08
       0.00000000e+00  0.00000000e+00  1.04606629e-07]
     [ 0.00000000e+00  0.00000000e+00  0.00000000e+00 -9.64183682e-08
       0.00000000e+00  0.00000000e+00  9.64183682e-08]
     [ 0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00
      -1.25633650e-07 -4.03926936e-07  0.00000000e+00]]
    [5, 8, 2]
    
        4
  •  0
  •   constantstranger    1 月前

    您可以通过以下方式执行此操作:

    mask = (A == 0).all(1)
    B = np.append(B, np.arange(A.shape[0])[mask])  # .tolist()
    A = A[~mask]