4

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.

Community
  • 1
  • 1
strpeter
  • 2,562
  • 3
  • 27
  • 48

1 Answers1

14

You can use a conditional expression to just return the ASCII code for c if the character is not present in alphabet:

sort(['RL','HH','DA','AH'], 
     key=lambda word: [alphabet.index(c) if c in alphabet else ord(c) for c in word])

I'd use a dictionary for alphabet instead, however, so you can use dict.get() here:

alphabet = {'H': 0, 'V': 1, 'R': 2, 'L': 3, 'D': 4, 'A': 5}
sort(['RL','HH','DA','AH'], 
     key=lambda word: [alphabet.get(c, ord(c)) for c in word])

Producing that dictionary from an input string is easy enough:

alphabet = {c: i for i, c in enumerate(alphabet_string)}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Very nice this is also case sensitive as it is expected. What is the advantage of a dictionary and its difference with respect to an alphabet? – strpeter Dec 02 '14 at 15:45
  • Or for fun, `alphabet = dict(zip(alphabet_string, itertools.count()))`. You could even start with a negative count, just to cover the case where there are more characters in the alphabet, than the smallest numeric value of any other character in the strings to be sorted. – Steve Jessop Dec 02 '14 at 15:48