Sort words in python

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

+6
python
source share
1 answer

You can usually define custom comparison methods so that sorting is within your limits. I have never encoded a Python string in my life, but it is similar to Ruby enough for it to notice that the following excerpt from this page may 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 can also check out these articles , Good luck!

+12
source share

All Articles