python3(十六) sorted

# sorted()函数list进行排序:
L = sorted([36, 5, -12, 9, -21])
print(L)  # [-21, -12, 5, 9, 36]
# 可以看到默认是按照升序排

LL = sorted([36, 5, -12, 9, -21], key=abs)
print(LL)  # [5, 9, -12, -21, 36]  自定义key 按照绝对值进行排序

lstr = sorted(['bob', 'about', 'Zoo', 'Credit'])
print(lstr)  # ['Credit', 'Zoo', 'about', 'bob']  默认按照ASCII的大小排序

lsortlower = sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower)
print(lsortlower)  # ['about', 'bob', 'Credit', 'Zoo']

lsortlowerRev = sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower, reverse=True)
print(lsortlowerRev)  # ['Zoo', 'Credit', 'bob', 'about']  按照原有规则反向排序

 

posted @ 2019-09-18 16:56  ~清风煮酒~  阅读(186)  评论(0编辑  收藏  举报