Python中的sort()方法使用基础教程
作者:路永远在脚下 时间:2022-03-07 21:44:09
一、基本形式
sorted(iterable[, cmp[, key[, reverse]]])
iterable.sort(cmp[, key[, reverse]])
参数解释:
(1)iterable指定要排序的list或者iterable,不用多说;
(2)cmp为函数,指定排序时进行比较的函数,可以指定一个函数或者lambda函数,如:
students为类对象的list,没个成员有三个域,用sorted进行比较时可以自己定cmp函数,例如这里要通过比较第三个数据成员来排序,代码可以这样写:
students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
sorted(students, key=lambda student : student[2])
(3)key为函数,指定取待排序元素的哪一项进行排序,函数用上面的例子来说明,代码如下:
sorted(students, key=lambda student : student[2])
key指定的lambda函数功能是去元素student的第三个域(即:student[2]),因此sorted排序时,会以students所有元素的第三个域来进行排序。
二、普通用法:
1.原址排序
1)列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不可修改的。
x = [4, 6, 2, 1, 7, 9]
x.sort()
print x # [1, 2, 4, 6, 7, 9]
2.副本排序
1)[:]分片方法
x =[4, 6, 2, 1, 7, 9]
y = x[ : ]
y.sort()
print y #[1, 2, 4, 6, 7, 9]
print x #[4, 6, 2, 1, 7, 9]
注意:y = x[:] 通过分片操作将列表x的元素全部拷贝给y,如果简单的把x赋值给y:y = x,y和x还是指向同一个列表,并没有产生新的副本。
2)sorted方法
sorted返回一个有序的副本,并且类型总是列表,如下:
x =[4, 6, 2, 1, 7, 9]
y = sorted(x)
print y #[1, 2, 4, 6, 7, 9]
print x #[4, 6, 2, 1, 7, 9]
print sorted('Python') #['P', 'h', 'n', 'o', 't', 'y']
三、高级用法
1.自定义cmp比较函数
def comp(x, y):
if x < y:
return 1
elif x > y:
return -1
else:
return 0
nums = [3, 2, 8 ,0 , 1]
nums.sort(comp)
print nums # 降序排序[8, 3, 2, 1, 0]
nums.sort(cmp) # 调用内建函数cmp ,升序排序
print nums # 降序排序[0, 1, 2, 3, 8]
2.自定义key和reverse
1.reverse实现降序排序,需要提供一个布尔值,默认为False(升序排列)。
2.key在使用时必须提供一个排序过程总调用的函数:
alist = [('2', '3', '10'), ('1', '2', '3'), ('5', '6', '7'), ('2', '5', '10'), ('2', '4', '10')]
# 多级排序,先按照第3个元素排序,然后按照第2个元素排序:
print sorted(alist, cmp = None, key = lambda x:(int(x[2]), int(x[1])), reverse = False)
-------------------------------------------------------------------------------------------
[('1', '2', '3'), ('5', '6', '7'), ('2', '3', '10'), ('2', '4', '10'), ('2', '5', '10')]
四、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函数获取的不是值,而是定义了一个函数,通过该函数作用到对象上才能获取值。
itemgetter在sort中的用法:
from operator import itemgetter
alist = [('2', '3', '10'), ('1', '2', '3'), ('5', '6', '7'), ('2', '5', '10'), ('2', '4', '10')]
# 多级排序,先按照第3个元素排序,然后按照第2个元素排序:
print sorted(alist, cmp = None, key = itemgetter(2, 1), reverse = False)
print sorted(alist, cmp = None, key = lambda x:itemgetter(2, 1)(x), reverse = False)
print sorted(alist, cmp = None, key = lambda x:map(int, itemgetter(2, 1)(x)), reverse = False)
--------------------------------------------------------------------------------------------------
[('2', '3', '10'), ('2', '4', '10'), ('2', '5', '10'), ('1', '2', '3'), ('5', '6', '7')]
[('2', '3', '10'), ('2', '4', '10'), ('2', '5', '10'), ('1', '2', '3'), ('5', '6', '7')]
[('1', '2', '3'), ('5', '6', '7'), ('2', '3', '10'), ('2', '4', '10'), ('2', '5', '10')]
以上所述是小编给大家介绍的Python中的sort()方法使用基础 ,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
来源:http://www.cnblogs.com/sunny3312/archive/2017/01/07/6260472.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python用dilb提取照片上人脸的示例
![](https://img.aspxhome.com/file/2023/0/66410_0s.png)
Python3.6 + TensorFlow 安装配置图文教程(Windows 64 bit)
![](https://img.aspxhome.com/file/2023/1/81661_0s.png)
python中shell执行知识点
常见系统中文字体的英文名
python爬虫实例之获取动漫截图
![](https://img.aspxhome.com/file/2023/8/90878_0s.jpg)
python3 mmh3安装及使用方法
![](https://img.aspxhome.com/file/2023/7/79567_0s.png)
悟道WEB标准:统一思想,遵循标准
Safari显示网页字体为超级无敌难看的宋体的原因
python数据类型bytes 和 bytearray的使用与区别
Python实现截图生成符合markdown的链接
![](https://img.aspxhome.com/file/2023/9/67299_0s.png)
php的对象传值与引用传值代码实例讲解
![](https://img.aspxhome.com/file/2023/4/55714_0s.jpg)
PyCharm 2020 激活到 2100 年的教程
![](https://img.aspxhome.com/file/2023/7/80627_0s.jpg)
浅谈Python中的正则表达式
python微信跳一跳系列之自动计算跳一跳距离
![](https://img.aspxhome.com/file/2023/3/66813_0s.gif)
python中数组和矩阵乘法及使用总结(推荐)
“)”引起PNG透明滤镜失效
python tkinter模块的简单使用
![](https://img.aspxhome.com/file/2023/5/75325_0s.png)
Python切片操作实例分析
Django框架静态文件处理、中间件、上传文件操作实例详解
![](https://img.aspxhome.com/file/2023/0/68890_0s.png)