Find the row indexes of several values in a numpy array

前端 未结 6 1879
醉话见心
醉话见心 2020-11-22 02:01

I have an array X:

X = np.array([[4,  2],
              [9,  3],
              [8,  5],
              [3,  3],
              [5,  6]])

And

6条回答
  •  遇见更好的自我
    2020-11-22 02:28

    X = np.array([[4,  2],
                  [9,  3],
                  [8,  5],
                  [3,  3],
                  [5,  6]])
    
    S = np.array([[4, 2],
                  [3, 3],
                  [5, 6]])
    
    result = [[i for i,row in enumerate(X) if (s==row).all()] for s in S]
    

    or

    result = [i for s in S for i,row in enumerate(X) if (s==row).all()]
    

    if you want a flat list (assuming there is exactly one match per searched value).

提交回复
热议问题