您当前的位置:首页 > 计算机 > 编程开发 > Python

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

时间:12-24来源:作者:点击数:

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]

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门