Given:
R=["ip1", "ip7", "ip12", "ip5", "ip2", "ip22", "ip7", "ip1", "ip17", "ip22"]
I would like to get unique values of my list R with their corresponding indices.
Right now, I have name,idx=np.unique(R,return_inverse=True) which returns:
array(['ip1', 'ip12', 'ip17', 'ip2', 'ip22', 'ip5', 'ip7'], dtype='<U4') # name
[0 6 1 5 3 4 6 0 2 4] # idx
But I would like to use customized sorting with results as follows:
['ip1', 'ip2', 'ip5', 'ip7', 'ip12', 'ip17', 'ip22']
[0 3 4 2 1 6 3 0 5 6]
In list, I can use Rs=sorted(R, key=lambda x: int(x[2:])) with customized key but I can’t get unique values and corresponding indices.
Is there any way to manipulate sorting key np.unique or is there already a better approach handling this?
>Solution :
Run unique after conversion to int:
_, i, idx = np.unique([int(x[2:]) for x in R],
return_index=True,
return_inverse=True)
names = np.array(R)[i]
output:
# names
array(['ip1', 'ip2', 'ip5', 'ip7', 'ip12', 'ip17', 'ip22'], dtype='<U4')
# idx
array([0, 3, 4, 2, 1, 6, 3, 0, 5, 6])