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

后端 未结 8 1101
旧巷少年郎
旧巷少年郎 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条回答
  •  天涯浪人
    2020-12-02 18:45

    I would just do this:

    indices = np.where(y[:, None] == x[None, :])[1]
    

    Unlike your memory-hog way, this makes use of broadcast to directly generate 2D boolean array without creating 2D arrays for both x and y.

提交回复
热议问题