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

case-sensitive list sorting, but just the duplicate values?

I have a list of strings like this:
['A', 'b','C','adam','ADam','EVe','eve','Eve','d','Adam']

I need to sort only the duplicate values only in string order to get output as
['A', 'b','C','ADam','Adam','adam','EVe','Eve','eve','d']

Here ‘ADam’,’Adam’,’adam’ were originally at different places in the list, but by standard ordering, they should be like this. Hence when the sorting method sees ‘adam’, it should try to find duplicates, sort and reorder the list as in the output for all adam’s(Case Sensitive Order)
Please note all the other values remain as is. i.e ‘A’, ‘b’,’C’,’d’ all remain in original positions

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

I am able to do a standard sort or write complex code to do this work but I am looking for some existing and optimal mechanism as this list can be huge (Billions of records).So efficiency is crucial

Any ideas or pointers to existing library of code snippets helps
Thanks in advance.

>Solution :

Try:

lst = ["A", "b", "C", "adam", "ADam", "EVe", "eve", "Eve", "d", "Adam"]

tmp = {}
for i, word in enumerate(map(str.lower, lst)):
    if word not in tmp:
        tmp[word] = i

lst = sorted(lst, key=lambda w: (tmp[w.lower()], w))
print(lst)

Prints:

['A', 'b', 'C', 'ADam', 'Adam', 'adam', 'EVe', 'Eve', 'eve', 'd']
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