check for identical rows in different numpy arrays

后端 未结 6 809
无人共我
无人共我 2020-11-28 15:14

how do I get a row-wise comparison between two arrays, in the result of a row-wise true/false array?

Given datas:

a = np.array([[1,0],[2,0],[3,1],[         


        
6条回答
  •  野性不改
    2020-11-28 16:00

    Here's a vectorised solution:

    res = (a[:, None] == b).all(-1).any(-1)
    
    print(res)
    
    array([ True,  True, False,  True])
    

    Note that a[:, None] == b compares each row of a with b element-wise. We then use all + any to deduce if there are any rows which are all True for each sub-array:

    print(a[:, None] == b)
    
    [[[ True  True]
      [False  True]
      [False False]]
    
     [[False  True]
      [ True  True]
      [False False]]
    
     [[False False]
      [False False]
      [False False]]
    
     [[False False]
      [False False]
      [ True  True]]]
    

提交回复
热议问题