2010-06-14 71 views
6

是否有可能在python中排序不是根據英文字母表的單詞列表,而是根據自己創建的字母表排序。在python中排序詞

+1

什麼是自制字母表? – SilentGhost 2010-06-14 13:13:21

回答

12

您通常可以定義自定義比較方法,以便在您的限制內執行排序。我從來沒有編碼在我的生活線的Python的,但它足夠類似紅寶石,我注意到,從this page以下摘錄可以幫助你:

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

你可能也想看看thesearticles。祝你好運!

+0

感謝這些信息;它幫助我前進 – Preys 2010-06-14 21:06:36