Is it possible in python to sort a list of words not according to the english alphabet but according to a self created alphabet.
Asked
Active
Viewed 3,304 times
1 Answers
13
You can normally define custom comparison methods so the sort is performed within your restrictions. I've never coded a line of Python in my life, but it's similar enough to Ruby for me to notice that the following excerpt from this page might help you:
alphabet = "zyxwvutsrqpomnlkjihgfedcba"
inputWords = ["england", "france", "spain", "italy", "greece", "portugal",
"canada", "usa", "mexico", "peru", "cuba", "chile", "argentina",
"zimbabwe", "uganda", "congo", "zambia", "namibia", "ghana"]
print sorted(inputWords, key=lambda word: [alphabet.index(c) for c in word])

Jeriko
- 6,547
- 4
- 28
- 40