Assume I have an alphabet with a different order: {H,V,R,L,D,A}
. Now I want to order strings as 'HV'
according to this order. I was expecting something that should look like:
$ alphabet = 'HVRLDA'
$ sorted(['RL','HH','AH'], key=lambda word: [alphabet.index(c) for c in word])
['HH','RL','AH']
This is a task that was mentioned already in Sorting string values according to a custom alphabet in Python. If one of these strings contains a character outside this alphabet, the script aborts with the error message:
ValueError: substring not found
Question
I want Python to process also non appearing characters according to their ASCII code. In this sense the rest of the letters should be appended to this alphabet.
Thank you for your replies and I hope this question could help others too.