find and delete from more-dimensional numpy array

后端 未结 3 1143
梦毁少年i
梦毁少年i 2020-11-29 10:51

I have two numpy-arrays:

p_a_colors=np.array([[0,0,0],
                     [0,2,0],
                     [119,103,82],
                     [122,122,122],
          


        
3条回答
  •  無奈伤痛
    2020-11-29 11:08

    This is how I would do it:

    dtype = np.dtype((np.void, (p_a_colors.shape[1] * 
                                p_a_colors.dtype.itemsize)))
    mask = np.in1d(p_a_colors.view(dtype), p_rem.view(dtype))
    p_r_colors = p_a_colors[~mask]
    
    >>> p_r_colors
    array([[0, 0, 0],
           [0, 2, 0],
           [3, 2, 4]])
    

    You need to do the void dtype thing so that numpy compares rows as a whole. After that using the built-in set routines seems like the obvious way to go.

提交回复
热议问题