Get indices of intersecting rows of Numpy 2d Array

前端 未结 2 834
攒了一身酷
攒了一身酷 2020-12-19 07:59

I want to get the indices of the intersecting rows of a main numpy 2d array A, with another one B.

A=array([[1, 2],
         [3, 4],
         [5, 6],
                


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-19 08:29

    You can use np.char.array() objects to do this comparison using np.in1d():

    s1 = np.char.array(A[:,0]) + '-' + np.char.array(A[:,1])
    s2 = np.char.array(B[:,0]) + '-' + np.char.array(B[:,1])
    
    np.where(np.in1d(s1, s2))[0]
    #array([0, 2], dtype=int64)
    

    NOTE: A and B must be of the same data type (int, float, etc) for this to work.

提交回复
热议问题