[python]sorted()函数及用法

sorted() 作为 Python 内置函数之一,其功能是对序列(列表、元组、字典、集合、还包括字符串)进行排序。

sorted() 函数的基本语法格式如下:

list = sorted(iterable, key=None, reverse=False)

其中,iterable 表示指定的序列,key 参数可以自定义排序规则;reverse 参数指定以升序(False,默认)还是降序(True)进行排序。

sorted() 函数会返回一个排好序的列表。注意,key 参数和 reverse 参数是可选参数,即可以使用,也可以忽略。

下面程序演示了 sorted() 函数的基本用法:

复制代码
#对列表进行排序
a = [5,3,4,2,1]
print(sorted(a))

#对元组进行排序
a = (5,4,3,1,2)
print(sorted(a))

#字典默认按照key进行排序
a = {4:1,\
 5:2,\
 3:3,\
 2:6,\
 1:8}
print(sorted(a.items()))

#对集合进行排序
a = {1,5,3,2,4}
print(sorted(a))

#对字符串进行排序
a = "51423"
print(sorted(a))

程序执行结果为:[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[(1, 8), (2, 6), (3, 3), (4, 1), (5, 2)]
[1, 2, 3, 4, 5]
['1', '2', '3', '4', '5']

再次强调,使用 sorted() 函数对序列进行排序, 并不会在原序列的基础进行修改,而是会重新生成一个排好序的列表。例如:

复制代码
#对列表进行排序
a = [5,3,4,2,1]
print(sorted(a))
#再次输出原来的列表 a
print(a)
程序执行结果为:[1, 2, 3, 4, 5]
[5, 3, 4, 2, 1]

显然,sorted() 函数不会改变所传入的序列,而是返回一个新的、排序好的列表。

除此之外,sorted()函数默认对序列中元素进行升序排序,通过手动将其 reverse 参数值改为 True,可实现降序排序。例如:

复制代码
#对列表进行排序
a = [5,3,4,2,1]
print(sorted(a,reverse=True))
程序执行结果为:[5, 4, 3, 2, 1]

另外在调用 sorted() 函数时,还可传入一个 key 参数,它可以接受一个函数,该函数的功能是指定 sorted() 函数按照什么标准进行排序。例如:

复制代码
chars=['http://c.biancheng.net',\
 'http://c.biancheng.net/python/',\
 'http://c.biancheng.net/shell/',\
 'http://c.biancheng.net/java/',\
 'http://c.biancheng.net/golang/']
#默认排序
print(sorted(chars))

#自定义按照字符串长度排序
print(sorted(chars,key=lambda x:len(x)))

程序执行结果为:['http://c.biancheng.net',
 'http://c.biancheng.net/golang/',
 'http://c.biancheng.net/java/',
 'http://c.biancheng.net/python/',
 'http://c.biancheng.net/shell/']
['http://c.biancheng.net',
 'http://c.biancheng.net/java/',
 'http://c.biancheng.net/shell/',
 'http://c.biancheng.net/python/',
 'http://c.biancheng.net/golang/']

operator.itemgetter函数

operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子。

a = 1,2,3

>>> b=operator.itemgetter(1) //定义函数b,获取对象的第1个域的值

>>> b(a)

2

>>> b=operator.itemgetter(1,0) //定义函数b,获取对象的第1个域和第0个的值

>>> b(a)

(2, 1)

要注意,operator.itemgetter函数获取的不是值,而是定义了一个函数,通过该函数作用到对象上才能获取值。

key为函数,指定取待排序元素的哪一项进行排序,函数用上面的例子来说明,代码如下:

sorted(students, key=lambda student : student2)

key指定的lambda函数功能是去元素student的第三个域(即:student2),因此sorted排序时,会以students所有元素的第三个域来进行排序。

有了上面的operator.itemgetter函数,也可以用该函数来实现,例如要通过student的第三个域排序,可以这么写:

sorted(students, key=operator.itemgetter(2))

sorted函数也可以进行多级排序,例如要根据第二个域和第三个域进行排序,可以这么写:

sorted(students, key=operator.itemgetter(1,2))

即先跟句第二个域排序,再根据第三个域排序。

相关推荐
惊讶的猫2 小时前
工具选对之后还不够:商城 Agent 的参数与权限治理
开发语言·python
cmpxr_2 小时前
【MicroPython】如何刷新ESP8266的固件
开发语言·micropython
天赐范式2 小时前
天赐范式第186天:让方差开始自洽——Bulmer效应的闭式验证
python·数字生命·天赐范式·动态运行时·遗传方差·bulmer效应
泡茶喝茶写代码2 小时前
A股量化数据工程:从 REST 接口到策略信号(第 8 篇):成长能力因子:营收与利润增速
java·python·股票数据api·股票数据api接口·股票量化数据api·股票量化数据接口·股票数据api数据
搭贝2 小时前
上厕所的时间搭好一套系统:AI自动生成实测
开发语言·人工智能·低代码·ai·php·搭贝
智感子3 小时前
智能称重·数据上云:从仪表读数到可追溯的计量凭证
开发语言·php
\光辉岁月/3 小时前
7.javase-面向对象
java·开发语言
Είναι η κοπέλα3 小时前
显存计算与模型选择:你的显卡能跑多大的模型
人工智能·pytorch·python·开源·conda
weixin_531094693 小时前
heap_4内存管理
java·开发语言·算法
ROSF68683 小时前
2026 仿石漆乳液供应商:市场布局与行业发展态势梳理
大数据·人工智能·python