6
votes

Is it possible in python to sort a list of words not according to the english alphabet but according to a self created alphabet.

1
what is self-created alphabet? - SilentGhost

1 Answers

13
votes

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

You might also want to check out these articles. Good luck!