6

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

Preys
  • 121
  • 2
  • 6

1 Answers1

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

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

Jeriko
  • 6,547
  • 4
  • 28
  • 40