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

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]

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dongfuguo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值