Compare two numpy arrays row-wise ValueError

后端 未结 3 1194
余生分开走
余生分开走 2021-01-07 04:41

I want to compare two NumPy arrays row-wise and return the number of same rows.

If i use the code below:

a=np.array([[1,2],[3,4]])
b=np.         


        
3条回答
  •  粉色の甜心
    2021-01-07 05:24

    What about something like this:

    import numpy as np
    
    a = np.array([['a', 'b'], ['c', 'd'],\
                      ['e', 't'], ['a', 'b'], ['a', 'b']])
    [['a' 'b']
     ['c' 'd']
     ['e' 't']
     ['a' 'b']
     ['a' 'b']]
    
    b = np.array([['a','b'],['e','t'],['r','t']])
    [['a' 'b']
     ['e' 't']
     ['r' 't']]
    
    shared_rows=0
    
    for row in b:
        temp=a==row
        shared_rows+=sum(np.sum(temp, axis=1)==a.shape[1])
    
    print(shared_rows)
    4
    

提交回复
热议问题