I have two lists of strings like this (two lists have the same items but in different orders):
lst1 = ['C', 'H', 'H', 'H', 'C', 'O', 'O', 'H']
lst2 = ['H', 'H', 'C', 'O', 'H', 'O', 'H', 'C']
To sort lst2 based on lst1, I want to find the lst2 index for each item in lst1. For duplicates I want to take the smallest index that has not been taken.
I want to know the fastest way to get a index list like this:
ids = [2, 0, 1, 4, 7, 3, 5, 6]
then I can sort lst2 by
newlst2 = [lst2[i] for i in ids]
Since there are duplicates, the normal way of getting list indices
ids = [lst2.index(x) for x in lst1]
will give the wrong result
ids = [2, 0, 0, 0, 2, 3, 3, 0]
Any suggestions?