前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python内置函数sorted()从入门到精通

Python内置函数sorted()从入门到精通

作者头像
Python小屋屋主
发布2018-04-17 10:56:26
8000
发布2018-04-17 10:56:26
举报
文章被收录于专栏:Python小屋Python小屋

Python内置函数sorted()可以对列表、元组、字典、集合、字符串、range对象以及其他可迭代对象进行排序,返回排序后的列表,支持使用key参数指定排序规则,支持reverse参数指定升序或者降序

>>> sorted(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

#降序排列 >>> sorted(range(10), reverse=True) [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

#对字符串中的字符升序排序 >>> sorted('hello world') [' ', 'd', 'e', 'h', 'l', 'l', 'l', 'o', 'o', 'r', 'w'] >>> d = {'a':3, 'b':2, 'c':5, 'd':0}

#默认对字典中的键进行排序 >>> sorted(d) ['a', 'b', 'c', 'd'] >>> sorted(d.keys()) ['a', 'b', 'c', 'd']

#对字典中的值进行排序 >>> sorted(d.values()) [0, 2, 3, 5]

#对字典中的元素进行排序 >>> sorted(d.items()) [('a', 3), ('b', 2), ('c', 5), ('d', 0)]

>>> x = ['da', 'cc', 'aba', 'aec']

#按默认规则对字符串进行排序 >>> sorted(x) ['aba', 'aec', 'cc', 'da']

#按字符串长度进行排序 >>> sorted(x, key=len) ['da', 'cc', 'aba', 'aec']

#按字符串长度降序排序

>>> sorted(x, key=len, reverse=True) ['aba', 'aec', 'da', 'cc']

#按字符串长度降序排序

#注意负号的用法仅适用于数字 >>> sorted(x, key=lambda i:-len(i)) ['aba', 'aec', 'da', 'cc']

#按字符串下标为1的字符进行排序

>>> sorted(x, key=lambda i:i[1]) ['da', 'aba', 'cc', 'aec']

>>> from random import randint

#列表推导式,生成包含10个子列表的列表,每个子列表中包含10个随机数 >>> x = [[randint(1,10) for i in range(10)] for j in range(10)] >>> for item in x: print(item)

[4, 10, 4, 6, 7, 9, 5, 1, 7, 10] [7, 10, 10, 3, 2, 7, 9, 10, 5, 7] [8, 3, 9, 2, 9, 8, 10, 7, 6, 4] [6, 2, 6, 6, 8, 6, 5, 2, 10, 3] [9, 2, 3, 1, 7, 4, 10, 1, 5, 10] [8, 2, 1, 3, 7, 4, 9, 1, 1, 9] [8, 6, 2, 4, 6, 2, 9, 3, 1, 2] [8, 8, 3, 1, 1, 8, 6, 10, 10, 3] [10, 5, 8, 10, 7, 1, 8, 10, 3, 10] [3, 2, 9, 2, 3, 5, 4, 8, 1, 3]

#对所有子列表进行排序 >>> for item in sorted(x): print(item)

[3, 2, 9, 2, 3, 5, 4, 8, 1, 3] [4, 10, 4, 6, 7, 9, 5, 1, 7, 10] [6, 2, 6, 6, 8, 6, 5, 2, 10, 3] [7, 10, 10, 3, 2, 7, 9, 10, 5, 7] [8, 2, 1, 3, 7, 4, 9, 1, 1, 9] [8, 3, 9, 2, 9, 8, 10, 7, 6, 4] [8, 6, 2, 4, 6, 2, 9, 3, 1, 2] [8, 8, 3, 1, 1, 8, 6, 10, 10, 3] [9, 2, 3, 1, 7, 4, 10, 1, 5, 10] [10, 5, 8, 10, 7, 1, 8, 10, 3, 10]

#按每个子列表元素之和从小到大排序

>>> for item in sorted(x, key=sum):

print(sum(item), ':', item)

40 : [3, 2, 9, 2, 3, 5, 4, 8, 1, 3]

43 : [8, 6, 2, 4, 6, 2, 9, 3, 1, 2]

45 : [8, 2, 1, 3, 7, 4, 9, 1, 1, 9]

52 : [9, 2, 3, 1, 7, 4, 10, 1, 5, 10]

54 : [6, 2, 6, 6, 8, 6, 5, 2, 10, 3]

58 : [8, 8, 3, 1, 1, 8, 6, 10, 10, 3]

63 : [4, 10, 4, 6, 7, 9, 5, 1, 7, 10]

66 : [8, 3, 9, 2, 9, 8, 10, 7, 6, 4]

70 : [7, 10, 10, 3, 2, 7, 9, 10, 5, 7]

72 : [10, 5, 8, 10, 7, 1, 8, 10, 3, 10]

#按子列表中下标为1的元素进行排序 >>> for item in sorted(x, key=lambda i: i[1]): print(item)

[6, 2, 6, 6, 8, 6, 5, 2, 10, 3] [9, 2, 3, 1, 7, 4, 10, 1, 5, 10] [8, 2, 1, 3, 7, 4, 9, 1, 1, 9] [3, 2, 9, 2, 3, 5, 4, 8, 1, 3] [8, 3, 9, 2, 9, 8, 10, 7, 6, 4] [10, 5, 8, 10, 7, 1, 8, 10, 3, 10] [8, 6, 2, 4, 6, 2, 9, 3, 1, 2] [8, 8, 3, 1, 1, 8, 6, 10, 10, 3] [4, 10, 4, 6, 7, 9, 5, 1, 7, 10] [7, 10, 10, 3, 2, 7, 9, 10, 5, 7]

#按每个子列表前两项之和进行排序 >>> for item in sorted(x, key=lambda i: i[0]+i[1]): print(item)

[3, 2, 9, 2, 3, 5, 4, 8, 1, 3] [6, 2, 6, 6, 8, 6, 5, 2, 10, 3] [8, 2, 1, 3, 7, 4, 9, 1, 1, 9] [8, 3, 9, 2, 9, 8, 10, 7, 6, 4] [9, 2, 3, 1, 7, 4, 10, 1, 5, 10] [4, 10, 4, 6, 7, 9, 5, 1, 7, 10] [8, 6, 2, 4, 6, 2, 9, 3, 1, 2] [10, 5, 8, 10, 7, 1, 8, 10, 3, 10] [8, 8, 3, 1, 1, 8, 6, 10, 10, 3] [7, 10, 10, 3, 2, 7, 9, 10, 5, 7]

#先按子列表中下标为0的元素排序

#如果下标为0的元素相等,再按下标为1的元素排序 >>> for item in sorted(x, key=lambda i: (i[0], i[1])): print(item)

[3, 2, 9, 2, 3, 5, 4, 8, 1, 3] [4, 10, 4, 6, 7, 9, 5, 1, 7, 10] [6, 2, 6, 6, 8, 6, 5, 2, 10, 3] [7, 10, 10, 3, 2, 7, 9, 10, 5, 7] [8, 2, 1, 3, 7, 4, 9, 1, 1, 9] [8, 3, 9, 2, 9, 8, 10, 7, 6, 4] [8, 6, 2, 4, 6, 2, 9, 3, 1, 2] [8, 8, 3, 1, 1, 8, 6, 10, 10, 3] [9, 2, 3, 1, 7, 4, 10, 1, 5, 10] [10, 5, 8, 10, 7, 1, 8, 10, 3, 10]

#按所有子列表下标为0的元素升序、下标为1的元素降序进行排序 >>> for item in sorted(x, key=lambda i: (i[0], -i[1])): print(item)

[3, 2, 9, 2, 3, 5, 4, 8, 1, 3] [4, 10, 4, 6, 7, 9, 5, 1, 7, 10] [6, 2, 6, 6, 8, 6, 5, 2, 10, 3] [7, 10, 10, 3, 2, 7, 9, 10, 5, 7] [8, 8, 3, 1, 1, 8, 6, 10, 10, 3] [8, 6, 2, 4, 6, 2, 9, 3, 1, 2] [8, 3, 9, 2, 9, 8, 10, 7, 6, 4] [8, 2, 1, 3, 7, 4, 9, 1, 1, 9] [9, 2, 3, 1, 7, 4, 10, 1, 5, 10] [10, 5, 8, 10, 7, 1, 8, 10, 3, 10]

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-03-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python小屋 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档