• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python中对列表排序实例

Python中对列表排序实例

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python脚本编写实例,python项目实例,python开发网站实例,python开发实例,python实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

很多时候,我们需要对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.
</div>
以下是sort的具体实例。
实例1:
>>>L = [2,3,1,4]
>>>L.sort()
>>>L
>>>[1,2,3,4]
</div>
实例2:
>>>L = [2,3,1,4]
>>>L.sort(reverse=True)
>>>L
>>>[4,3,2,1]
</div>
实例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)]
</div>
实例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)]
</div>
实例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)]
</div>
实例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)]
</div>
以上给出了6中对List排序的方法,其中实例3.4.5.6能起到对以List item中的某一项
为比较关键字进行排序.
效率比较:
cmp < DSU < key
</div>
通过实验比较,方法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)]
</div>
我们看到,此时排序过的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)]
</div>
实例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)]
</div>
为什么实例8能够工作呢?原因在于tuple是的比较从左到右之一比较的,比较完第一个,如果
相等,比较第二个

</div>

您可能想查找下面的文章:

  • python脚本爬取字体文件的实现方法
  • Python只用40行代码编写的计算器实例
  • Python实现脚本锁功能(同时只能执行一个脚本)
  • python脚本爬取字体文件的实现方法
  • python获取当前运行函数名称的方法实例代码
  • Python类的动态修改的实例方法
  • Python中动态创建类实例的方法
  • Python自动发邮件脚本
  • Python类的动态修改的实例方法
  • Python中动态创建类实例的方法

相关文章

  • 详解Django中的权限和组以及消息
  • Python标准库urllib2的一些使用细节总结
  • 用Python实现web端用户登录和注册功能的教程
  • 详细解析Python当中的数据类型和变量
  • Python实现的生成自我描述脚本分享(很有意思的程序)
  • 一个简单的python程序实例(通讯录)
  • 在Python中处理字符串之isdecimal()方法的使用
  • 使用Python简单的实现树莓派的WEB控制
  • python数据清洗系列之字符串处理详解
  • 在Linux系统上部署Apache+Python+Django+MySQL环境

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python+Wordpress制作小说站
    • Pycharm学习教程(2) 代码风格
    • python中set常用操作汇总
    • 跟老齐学Python之Python安装
    • Python3.2中的字符串函数学习总结
    • Python中使用Beautiful Soup库的超详细教程
    • Python 递归函数详解及实例
    • python在Windows8下获取本机ip地址的方法
    • 使用Python编写Linux系统守护进程实例
    • python采用requests库模拟登录和抓取数据的简单示例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有