Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

numpy unique with customized sorting of unique elements of an array

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading