Find the row indexes of several values in a numpy array

前端 未结 6 1938
醉话见心
醉话见心 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

    Another way is to use cdist function from scipy.spatial.distance like this:

        np.nonzero(cdist(X, searched_values) == 0)[0]
    

    Basically, we get row numbers of X which have distance zero to a row in searched_values, meaning they are equal. Makes sense if you look on rows as coordinates.

提交回复
热议问题