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.

1
  • 1
    what is self-created alphabet? Commented Jun 14, 2010 at 13:13

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.