您通常可以定义自定义比较方法,以便在您的限制范围内执行排序。我一生中从未编写过一行 Python 代码,但它与 Ruby 非常相似,让我注意到以下摘录自这一页 http://refactormycode.com/codes/179-custom-sort-a-list-of-words可能会帮助你:
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])
您可能还想查看these http://www.universalwebservices.net/web-programming-resources/python/advanced-sorting-techniques articles http://icfun.blogspot.com/2009/08/python-customize-sorting-with.html。祝你好运!