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
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']