Skip to content Skip to sidebar Skip to footer

Numpy To Get The Exact Arguments Of Duplicated Elements In A 2d Array

I have two 2D arrays a and b. I want to find the exact indices of a in b. I followed the solution proposed here. The problem is that my arrays contain duplicates as you can see her

Solution 1:

We could add one more column of IDs to represent duplicates within the rows and then use the same steps. We will use pandas to get those IDs, it's just easier that way. Hence, simply do -

import pandas as pd

def assign_duplbl(a):
    df = pd.DataFrame(a)
    df['num'] = 1
    return df.groupby(list(range(a.shape[1]))).cumsum().values

a1 = np.hstack((a,assign_duplbl(a)))
b1 = np.hstack((b,assign_duplbl(b)))
args0, args1 = argwhere_nd_searchsorted(a1,b1)

Post a Comment for "Numpy To Get The Exact Arguments Of Duplicated Elements In A 2d Array"