Python中对列表排序实例


Posted in Python onJanuary 04, 2015

很多时候,我们需要对List进行排序,Python提供了两个方法,对给定的List L进行排序:

方法1.用List的成员函数sort进行排序
方法2.用built-in函数sorted进行排序(从2.4开始)

这两种方法使用起来差不多,以第一种为例进行讲解:

从Python2.4开始,sort方法有了三个可选的参数,Python Library Reference里是这样描述的

cmp:cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: 

"cmp=lambda x,y: cmp(x.lower(), y.lower())" 

key:key specifies a function of one argument that is used to extract a comparison key from each list element: "key=str.lower"

reverse:reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.In general, the key and reverse conversion processes are much faster than specifying an 

equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once.

以下是sort的具体实例。
实例1:
>>>L = [2,3,1,4]

>>>L.sort()

>>>L

>>>[1,2,3,4]

实例2:
>>>L = [2,3,1,4]

>>>L.sort(reverse=True)

>>>L

>>>[4,3,2,1]

实例3:
>>>L = [('b',2),('a',1),('c',3),('d',4)]

>>>L.sort(cmp=lambda x,y:cmp(x[1],y[1]))

>>>L

>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

实例4:
>>>L = [('b',2),('a',1),('c',3),('d',4)]

>>>L.sort(key=lambda x:x[1])

>>>L

>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

实例5:
>>>L = [('b',2),('a',1),('c',3),('d',4)]

>>>import operator

>>>L.sort(key=operator.itemgetter(1))

>>>L

>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

实例6:(DSU方法:Decorate-Sort-Undercorate)
>>>L = [('b',2),('a',1),('c',3),('d',4)]

>>>A = [(x[1],i,x) for i,x in enumerate(L)] #i can confirm the stable sort

>>>A.sort()

>>>L = [s[2] for s in A]

>>>L

>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

以上给出了6中对List排序的方法,其中实例3.4.5.6能起到对以List item中的某一项
为比较关键字进行排序.
效率比较:
cmp < DSU < key

通过实验比较,方法3比方法6要慢,方法6比方法4要慢,方法4和方法5基本相当
多关键字比较排序:
实例7:
>>>L = [('d',2),('a',4),('b',3),('c',2)]

>>> L.sort(key=lambda x:x[1])

>>> L

>>>[('d', 2), ('c', 2), ('b', 3), ('a', 4)]

我们看到,此时排序过的L是仅仅按照第二个关键字来排的,如果我们想用第二个关键字
排过序后再用第一个关键字进行排序呢?有两种方法
实例8:
>>> L = [('d',2),('a',4),('b',3),('c',2)]

>>> L.sort(key=lambda x:(x[1],x[0]))

>>> L

>>>[('c', 2), ('d', 2), ('b', 3), ('a', 4)]

实例9:
>>> L = [('d',2),('a',4),('b',3),('c',2)]

>>> L.sort(key=operator.itemgetter(1,0))

>>> L

>>>[('c', 2), ('d', 2), ('b', 3), ('a', 4)]

为什么实例8能够工作呢?原因在于tuple是的比较从左到右之一比较的,比较完第一个,如果
相等,比较第二个
Python 相关文章推荐
python简单文本处理的方法
Jul 10 Python
对Python 3.2 迭代器的next函数实例讲解
Oct 18 Python
Python定时发送消息的脚本:每天跟你女朋友说晚安
Oct 21 Python
浅谈Python爬虫基本套路
Mar 25 Python
使用selenium模拟登录解决滑块验证问题的实现
May 10 Python
Python何时应该使用Lambda函数
Jul 02 Python
详解使用Python下载文件的几种方法
Oct 13 Python
利用PyCharm操作Github(仓库新建、更新,代码回滚)
Dec 18 Python
python实现多进程按序号批量修改文件名的方法示例
Dec 30 Python
python实现IOU计算案例
Apr 12 Python
python 使用raw socket进行TCP SYN扫描实例
May 05 Python
Python+OpenCV图像处理——图像二值化的实现
Oct 24 Python
Python实现爬取知乎神回复简单爬虫代码分享
Jan 04 #Python
Python连接mssql数据库编码问题解决方法
Jan 01 #Python
Python中optparse模块使用浅析
Jan 01 #Python
Python中urllib2模块的8个使用细节分享
Jan 01 #Python
Python读取ini文件、操作mysql、发送邮件实例
Jan 01 #Python
Python实现检测服务器是否可以ping通的2种方法
Jan 01 #Python
Python Web框架Flask信号机制(signals)介绍
Jan 01 #Python
You might like
点评山进PR-D3L三波段收音机
2021/03/02 无线电
Windows和Linux中php代码调试工具Xdebug的安装与配置详解
2014/05/08 PHP
thinkphp分页集成实例
2017/07/24 PHP
PHP copy函数使用案例代码解析
2020/09/01 PHP
css动画效果之animation的常用样式
2021/03/09 HTML / CSS
Javascript中的var_dump函数实现代码
2009/09/07 Javascript
select 控制网页内容隐藏于显示的实现代码
2010/05/25 Javascript
JS获取各种浏览器窗口大小的方法
2014/01/14 Javascript
jQuery选择器源码解读(二):select方法
2015/03/31 Javascript
纯JS代码实现气泡效果
2016/05/04 Javascript
node.js入门学习之url模块
2017/02/25 Javascript
jQuery时间验证和转换为标准格式的时间格式
2017/03/06 Javascript
JavaScript之Canvas_动力节点Java学院整理
2017/07/04 Javascript
JS实现发送短信验证后按钮倒计时功能(防止刷新倒计时失效)
2017/07/07 Javascript
Webpack优化配置缩小文件搜索范围
2017/12/25 Javascript
create-react-app构建项目慢的解决方法
2018/03/14 Javascript
Vue 项目代理设置的优化
2018/04/17 Javascript
微信小程序实现选项卡效果
2018/11/06 Javascript
Vue实现购物车的全选、单选、显示商品价格代码实例
2019/05/06 Javascript
JavaScript实现单英文金山打字通
2020/07/24 Javascript
JS实现求字符串中出现最多次数的字符和次数示例
2019/07/05 Javascript
layui-table对返回的数据进行转变显示的实例
2019/09/04 Javascript
通过实例解析JavaScript常用排序算法
2020/09/02 Javascript
[54:27]TNC vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
跟老齐学Python之使用Python查询更新数据库
2014/11/25 Python
Python实现自动发送邮件功能
2021/03/02 Python
python随机模块random的22种函数(小结)
2020/05/15 Python
用html5绘制折线图的实例代码
2016/03/25 HTML / CSS
html5自动播放mov格式视频的实例代码
2020/01/14 HTML / CSS
PyQt QMainWindow的使用示例
2021/03/24 Python
美食节策划方案
2014/05/26 职场文书
个人整改措施书面材料
2014/10/24 职场文书
信息技术研修心得体会
2016/01/08 职场文书
《田忌赛马》教学反思
2016/02/19 职场文书
导游词之宁夏贺兰山岩画
2019/11/08 职场文书
教你用python实现一个无界面的小型图书管理系统
2021/05/21 Python