Numpy: For every element in one array, find the index in another array

后端 未结 8 1056
旧巷少年郎
旧巷少年郎 2020-12-02 18:36

I have two 1D arrays, x & y, one smaller than the other. I\'m trying to find the index of every element of y in x.

I\'ve found two naive ways to do this, the fir

8条回答
  •  萌比男神i
    2020-12-02 18:53

    I want to suggest one-line solution:

    indices = np.where(np.in1d(x, y))[0]
    

    The result is an array with indices for x array which corresponds to elements from y which were found in x.

    One can use it without numpy.where if needs.

提交回复
热议问题