Python中sort以及sorted函数初探
sorted(...)
Help on built-in function sorted in module __builtin__:
sorted(...)
sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
sort(...)
Help on built-in function sort:
sort(...)
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1
sorted与sort的差别
1. sorted函数是内建函数,而sort是序列的内部函数,所以它们调用方式不一样,另外sorted函数多了一个系列迭代器參数
2. sorted函数不改变參数系列。可是返回排好序的序列副本;而sort作为序列的内部函数,调用完后会对调用的序列进行排序
以下的结果非常好的说明了这些:
>>> list=[2,1]
>>> x=sorted(list)
>>> x
[1, 2]
>>> list
[2, 1]
>>> y=list.sort()
>>> y
>>> list
[1, 2]
sorted与sort的參数
sorted与sort除了一个是序列作为參数,一个是序列调用该函数。其它參数差点儿全然一致,以下逐一来介绍其使用方法及效果:
1. 默认使用方法
因为sort函数的參数reverse,key,cmp都提供了缺省參数,所以我们能够直接不指定这些參数值调用该函数。
可是它必须有一个前提。就是list中存放的类型是可比較的。否则就会弹出错误“Type Error: unorderable type"。
2. reverse參数
当取值为True时候就是倒序排。默觉得False正序从小到大排
>>> list.sort(reverse=False)
>>> list
[1, 2]
>>> list.sort(reverse=True)
>>> list
[2, 1]
3. key參数
# Definition for an interval.
class Interval:
def __init__(self, s=0, e=0):
self.start = s
self.end = e # Initialize the Interval list
list = []
for i in range(10,7,-1):
for j in range(11,i,-1):
list.append(Interval(i,j))
这里我们定义了Interval为[s,e]的数据结构而且初始化了。对于这个问题。显然我们用缺省的參数来调用会出错。由于我们没有提供可比較的函数来比較类型Interval。
对于这种情况,我们就能够指定比較的key来解决。
#Sort the Interval list
list2 = sorted(list,key=lambda x:x.start) #Output the Interval list
for x in list:
print("[%d,%d]"%(x.start,x.end))
for x in list2:
print("[%d,%d]"%(x.start,x.end))
这里我们基于Interval.start作为key进行排序了。
#Sort the Interval list based on Interval.start and Interval.end
list2 = sorted(list,key=lambda x:(x.start,x.end))
我们用元祖(Interval.start,Interval.end)作为key来比較。而元祖有默认的cmp函数。这就达到了目标。
4. cmp參数
#Sort the Interval list based on Interval.start and Interval.end
def cmpInterval(a, b):
if a.start != b.start:
return cmp(a.start,b.start)
return cmp(a.end,b.end)
list1 = sorted(list,cmp = cmpInterval)
list2 = sorted(list,cmp = lambda x,y:cmp(x.start,y.start))
只是比較遗憾的是发如今python 3.x中传入cmp函数会出现一个错误:TypeError:
'cmp' is an invalid keyword argument for this function
Python中sort以及sorted函数初探的更多相关文章
- Python中sort和sorted函数代码解析
Python中sort和sorted函数代码解析 本文研究的主要是Python中sort和sorted函数的相关内容,具体如下. 一.sort函数 sort函数是序列的内部函数 函数原型: L.sor ...
- Python中sort与sorted函数
python中列表的内置函数sort()可以对列表中的元素进行排序,而全局性的sorted()函数则对所有可迭代的序列都是适用的: 并且sort()函数是内置函数,会改变当前对象,而sorted()函 ...
- Python中sort、sorted的cmp参数废弃之后使用cmp_to_key实现类似功能
Python2.1以前的排序比较方法只提供一个cmp比较函数参数,没有__lt__等6个富比较方法, Python 2.1引入了富比较方法,Python3.4之后作废了cmp参数.相应地从Python ...
- python中sort和sorted排序的相关方法
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. 1)排序基础 简单的升序排序是非常容易的.只需要调用sorte ...
- python中sort()与sorted()的区别
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_list = [3 ...
- python中sort和sorted用法的区别
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_list = [3 ...
- Python中sort、sorted的cmp参数废弃之后使用__lt__支持复杂比较的方法
Python2.1以前的排序比较方法只提供一个cmp比较函数参数,没有__lt__等6个富比较方法, Python 2.1引入了富比较方法,Python3.4之后作废了cmp参数.相应地从Python ...
- python中sort与sorted区别
1.sort()函数 (只对list有用) sort(...) L.sort(key = None,reverse=False) key = 函数 这个函数会从每个元素中提取一个用于比较的关键字.默认 ...
- 为什么Python中sort方法和sorted函数调用废弃使用cmp参数
Python中sort方法和sorted函数老猿在前面一些章节介绍过,具体语法及含义在此不再展开说明,但老猿在前面学习相关内容时,只使用了简单的案例,对这两个方法的key参数没有深入研究,总以为就是以 ...
随机推荐
- JNI_Android项目中调用.so动态库
JNI_Android项目中调用.so动态库 2014年6月3日 JNI学习 參考:http://blog.sina.com.cn/s/blog_4298002e01013zk8.html 上一篇笔者 ...
- Python 爬取汽车领域问答语料(自用)
#coding=utf-8 import time import requests from lxml import etree from pymongo import MongoClient fro ...
- TestNG 八 并发测试
一. Concurrenttesting: 下面的例子是输出进程ID,threadPoolSize用来指明线程池的大小,也就是并发的线程数目是多少 5次调用,有3个线程可调用 @Test(invoca ...
- weblogic 异常 com.rsa.jsafe.JSAFE_PaddingException: Could not perform unpadding: invalid pad byte.次异常怎么解决
问题 更改控制台密码后,服务重启失败,无法启动,报错如下: <-- 下午03时10分49秒 CST> <Info> <WebLogicServer> <BEA ...
- 【ACM】How many prime numbers
http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2§ionid=1&problemid=2 #inclu ...
- dhclient 简介
dhclient 就和它名字一样,用来通过 dhcp 协议配置本机的网络接口. 使用方法就是 #dhclient ifN # ifN 就是 ifconfig 中输出的接口名称,etc. eth0,wl ...
- 使用HTML5画布(canvas)生成阴影效果
来源:GBin1.com 使用HTML5的画布特性,我们可以创建图形,在这片文章中,我们将创建图形的阴影. var canvas = document.getElementById('shadowca ...
- Ubuntu 下的webservices
搞 了一下午: 开发server程序.需使用gSOAP生成server端代码框架. 我们有两种做法: 编写WSDL,使用wsdl2h生成头文件,再soapcpp2生成框架代码. 编写头文件.使用soa ...
- WindowManager.LayoutParams全解
WindowManager是Android中一个重要的服务(Service ).WindowManager Service 是全局的,是唯一的.它将用户的操作,翻译成为指令,发送给呈现在界面上的各个W ...
- 搭建 SMTP mail
邮件协议需要配置client 端 和 server 端,在linux redhat 下 client 端: 使用linux 自带的Evolution,2.12.3, 主要配置在preferrence ...