[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 : student[2])

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

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

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

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

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

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

相关推荐
2301_764441332 分钟前
水星热演化核幔耦合数值模拟
python·算法·数学建模
循环过三天3 分钟前
3.4、Python-集合
开发语言·笔记·python·学习·算法
Q_Q51100828525 分钟前
python+django/flask的眼科患者随访管理系统 AI智能模型
spring boot·python·django·flask·node.js·php
_院长大人_2 小时前
设计模式-工厂模式
java·开发语言·设计模式
MATLAB代码顾问2 小时前
MATLAB实现决策树数值预测
开发语言·决策树·matlab
SunnyDays10112 小时前
如何使用Python高效转换Excel到HTML
python·excel转html
Q_Q5110082852 小时前
python+django/flask的在线学习系统的设计与实现 积分兑换礼物
spring boot·python·django·flask·node.js·php
不染尘.3 小时前
2025_11_7_刷题
开发语言·c++·vscode·算法
似水এ᭄往昔3 小时前
【C++】--stack和queue
开发语言·c++
Q_Q5110082853 小时前
python+django/flask的车辆尾气检测排放系统-可视化大屏展示
spring boot·python·django·flask·node.js·php