python 【自定义排序】

 1 def main():
 2     list1 = ["demonstrate","aviation","strip","disastrous","tile"]
 3     list1.sort(key=len)
 4     print("Sorted by length in ascending order:")
 5     print(list1,'\n')
 6     list1.sort(key=lastCharacter)
 7     print("Sore=ted by last character in ascending order:")
 8     print(list1,'\n')
 9     list1.sort(key=numberOfVowels,reverse=True)
10     print("Sore=ted by last character in ascending order:")
11     print(list1)
12 #按结尾元音排序
13 def lastCharacter(word):
14     return word[-1]
15 #按元音个数排序
16 def numberOfVowels(word):
17     vowels = ('a','e','i','o','u')
18     total =0
19     for vowel in vowels:
20         total += word.count(vowel)
21     return total
22 main()        

 

posted @ 2018-03-26 21:54  Justice-V  阅读(515)  评论(0)    收藏  举报