python排序函数sort()与sorted()的区别
作者:故乡月zyl 时间:2023-09-08 23:38:08
python 中sorted与sort有什么区别
sort(cmp=None, key=None, reverse=False)
sorted(iterable, cmp=None, key=None, reverse=False)
sort是容器的函数,用List的成员函数sort进行排序
sorted是Python的内建函数相同的参数,用built-in函数sorted进行排序
sorted(iterable,key=None,reverse=False),返回新的列表,对所有可迭代的对象均有效
sort(key=None,reverse=False) 就地改变列表 reverse:True反序;False 正序
例如:
>>> a=[1,3,5,2,9,4,7,8,6,0]
>>> a.sort()
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a=[1,3,5,2,9,4,7,8,6,0]
>>> sorted(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a
[1, 3, 5, 2, 9, 4, 7, 8, 6, 0]
以下是详细说明
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列。
sorted(iterable,key=None,reverse=False),返回新的列表,对所有可迭代的对象均有效
sort(key=None,reverse=False) 就地改变列表 reverse:True反序;False 正序
sort是容器的函数:sort(cmp=None, key=None, reverse=False)
sorted是python的内建函数:sorted(iterable, cmp=None, key=None, reverse=False)
参数解析:
cmp:比较函数,比较什么参数由key决定。例如:cmp(e1, e2) 是具有两个参数的比较函数,返回值:负数(e1 < e2);0(e1 == e2);正数( e1 > e2)。
key:用列表元素的某个属性或函数作为关键字。
reverse:排序规则,可以选择True或者False。
sorted多一个参数iterable:待排序的可迭代类型的容器
示例1:
>>> a=[1,2,5,3,9,4,6,8,7,0,12]
>>> a.sort()
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
>>> a=[1,2,5,3,9,4,6,8,7,0,12]
>>> sorted(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
>>> a
[1, 2, 5, 3, 9, 4, 6, 8, 7, 0, 12]
区别:
对于一个无序的列表a,调用a.sort(),对a进行排序后返回a,sort()函数修改待排序的列表内容。
而对于同样一个无序的列表a,调用sorted(a),对a进行排序后返回一个新的列表,而对a不产生影响。
示例2:
假设用元组保存每一个学生的信息,包括学号,姓名,年龄。用列表保存所有学生的信息。
>>> list1=[(8, 'Logan', 20), (2, 'Mike', 22), (5, 'Lucy', 19)]
>>> list1.sort()
>>> list1
[(2, 'Mike', 22), (5, 'Lucy', 19), (8, 'Logan', 20)]
>>> list1=[(8, 'Logan', 20), (2, 'Mike', 22), (5, 'Lucy', 19)]
>>> sorted(list1)
[(2, 'Mike', 22), (5, 'Lucy', 19), (8, 'Logan', 20)]
>>> list1
[(8, 'Logan', 20), (2, 'Mike', 22), (5, 'Lucy', 19)]
小结:由示例可以看出,当列表由list(或者tuple)组成时,默认情况下,sort和sorted都会根据list[0](或者tuple[0])作为排序的key,进行排序。
以上都是默认的排序方式,我们可以编写代码控制两个函数的排序行为。主要有三种方式:基于key函数;基于cmp函数和基于reverse函数
1)基于key函数排序
>>> list1=[(8, 'Logan', 20), (2, 'Mike', 22), (5, 'Lucy', 19)]
>>> list1.sort(key=lambda x:x[2])
>>> list1
[(5, 'Lucy', 19), (8, 'Logan', 20), (2, 'Mike', 22)]
>>> list1=[(8, 'Logan', 20), (2, 'Mike', 22), (5, 'Lucy', 19)]
>>> sorted(list1, key=lambda x:x[2])
[(5, 'Lucy', 19), (8, 'Logan', 20), (2, 'Mike', 22)]
>>> list1
[(8, 'Logan', 20), (2, 'Mike', 22), (5, 'Lucy', 19)]
2)基于cmp函数
>>> list1=[(8, 'Zogan', 20), (2, 'Mike', 22), (5, 'Lucy', 19)]
>>> list1.sort(cmp=lambda x,y:cmp(x[1],y[1]))
>>> list1
[(5, 'Lucy', 19), (2, 'Mike', 22), (8, 'Zogan', 20)]
>>> list1=[(8, 'Zogan', 20), (2, 'Mike', 22), (5, 'Lucy', 19)]
>>> sorted(list1, cmp=lambda x,y:cmp(x[1],y[1]))
[(5, 'Lucy', 19), (2, 'Mike', 22), (8, 'Zogan', 20)]
>>> list1
[(8, 'Zogan', 20), (2, 'Mike', 22), (5, 'Lucy', 19)]
3)基于reverse函数
>>> a=[1,2,5,3,9,4,6,8,7,0,12]
>>> a.sort(reverse=False)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
>>> a=[1,2,5,3,9,4,6,8,7,0,12]
>>> a.sort(reverse=True)
>>> a
[12, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> a.sort(reverse=True)
小结:reverse=False为升序排序;reverse=True为降序排序
来源:https://blog.csdn.net/zyl1042635242/article/details/43115675
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
超强多功能php绿色集成环境详解
![](https://img.aspxhome.com/file/2023/2/55472_0s.jpg)
Python中异常捕获与处理的方法总结
![](https://img.aspxhome.com/file/2023/8/95898_0s.png)
谈ASP的未来
Python 选择排序中的树形选择排序
![](https://img.aspxhome.com/file/2023/3/75893_0s.jpg)
Python语音合成的项目实战(PyQt5+pyttsx3)
![](https://img.aspxhome.com/file/2023/9/128549_0s.jpg)
sql 自定义百分比转换小数函数代码
最好的Python DateTime 库之 Pendulum 长篇解析
![](https://img.aspxhome.com/file/2023/6/131466_0s.png)
SQL Server自定义异常raiserror使用示例
Javascript获取background属性中url的值
超级给力的JavaScript的React框架入门教程
Python 6种基本变量操作技巧总结
![](https://img.aspxhome.com/file/2023/2/97412_0s.jpg)
Python入门Anaconda和Pycharm的安装和配置详解
![](https://img.aspxhome.com/file/2023/5/82925_0s.png)
关于“简单,可依赖”
Python bisect模块原理及常见实例
Python进阶_关于命名空间与作用域(详解)
python 中的 super详解
![](https://img.aspxhome.com/file/2023/3/131563_0s.png)
pytorch DistributedDataParallel 多卡训练结果变差的解决方案
![](https://img.aspxhome.com/file/2023/7/95317_0s.jpg)
Git下载与环境变量配置方法
![](https://img.aspxhome.com/file/2023/0/132510_0s.jpg)