Index mapping between two sorted partially overlapping numpy arrays

前端 未结 1 1271
时光取名叫无心
时光取名叫无心 2021-01-28 12:55

I want to solve something like the problem detailed at Find index mapping between two numpy arrays, but where the two arrays do not necessarily contain the same set of values, a

1条回答
  •  伪装坚强ぢ
    2021-01-28 13:20

    Use the searchsorted indices to do a check on matches and then mask the invalid ones with the invalid-specifier. For the matching check, do b[idx]==a with idx as those indices. Hence -

    invalid_specifier = -1
    idx = np.searchsorted(b,a)
    idx[idx==len(b)] = 0
    out = np.where(b[idx]==a, idx, invalid_specifier)
    

    0 讨论(0)
提交回复
热议问题