排序算法

sorted - 廖雪峰

 

 1 L1 = sorted([36,5,-12,9,-21])
 2 print(L1)
 3 
 4 L2 = sorted([36,5,-12,9,-21],key=abs)
 5 print(L2)
 6 
 7 L3 = sorted(['bob', 'about', 'Zoo', 'Credit'])
 8 print(L3)
 9 
10 L4 = sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower)
11 print(L4)
12 
13 L5 = sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower, reverse=True)
14 print(L5)
15 
16 L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
17 
18 def by_name(t):
19     return t[0]
20 
21 def by_score(t):
22     return t[1]
23 
24 L6 = sorted(L,key=by_name)
25 L7 = sorted(L,key=by_score,reverse=True)
26 print(L6)
27 print(L7)

 

posted @ 2017-08-25 21:00  绝望的老猫  阅读(220)  评论(0编辑  收藏  举报