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參数

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进行排序了。


但是接着问题来了。假设我不仅比較Interval.start,当Interval.start相等时候,还想比較Interval.end,该怎么办?
#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參数

我们能够通过自己定义函数或则使用简洁的lambda来作为參数传给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

这时候我们就须要使用key来绕过这个问题。另外一个建议就是我们尽量使用key而不是cmp来排序以提高执行速度。

Python中sort以及sorted函数初探的更多相关文章

  1. Python中sort和sorted函数代码解析

    Python中sort和sorted函数代码解析 本文研究的主要是Python中sort和sorted函数的相关内容,具体如下. 一.sort函数 sort函数是序列的内部函数 函数原型: L.sor ...

  2. Python中sort与sorted函数

    python中列表的内置函数sort()可以对列表中的元素进行排序,而全局性的sorted()函数则对所有可迭代的序列都是适用的: 并且sort()函数是内置函数,会改变当前对象,而sorted()函 ...

  3. Python中sort、sorted的cmp参数废弃之后使用cmp_to_key实现类似功能

    Python2.1以前的排序比较方法只提供一个cmp比较函数参数,没有__lt__等6个富比较方法, Python 2.1引入了富比较方法,Python3.4之后作废了cmp参数.相应地从Python ...

  4. python中sort和sorted排序的相关方法

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. 1)排序基础 简单的升序排序是非常容易的.只需要调用sorte ...

  5. python中sort()与sorted()的区别

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_list = [3 ...

  6. python中sort和sorted用法的区别

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_list = [3 ...

  7. Python中sort、sorted的cmp参数废弃之后使用__lt__支持复杂比较的方法

    Python2.1以前的排序比较方法只提供一个cmp比较函数参数,没有__lt__等6个富比较方法, Python 2.1引入了富比较方法,Python3.4之后作废了cmp参数.相应地从Python ...

  8. python中sort与sorted区别

    1.sort()函数 (只对list有用) sort(...) L.sort(key = None,reverse=False) key = 函数 这个函数会从每个元素中提取一个用于比较的关键字.默认 ...

  9. 为什么Python中sort方法和sorted函数调用废弃使用cmp参数

    Python中sort方法和sorted函数老猿在前面一些章节介绍过,具体语法及含义在此不再展开说明,但老猿在前面学习相关内容时,只使用了简单的案例,对这两个方法的key参数没有深入研究,总以为就是以 ...

随机推荐

  1. Oracle 命令导入数据

    1.用命令进入sqlplus: sqlplus 用户名:/orcl 2.执行sql文件 sql>@D:/lanxi_his_data/V_JH_VISITINFO.sql

  2. RocketMQ里的一个获取时间的工具类SystemClock

    看rocketmq源码的时候发现他们还给时钟封装里一下. /** * Licensed to the Apache Software Foundation (ASF) under one or mor ...

  3. Visual Studio Image Library

    The Visual Studio Image Library Visual Studio 2013   The Visual Studio Image Library contains applic ...

  4. Win7无法安装Flash Player怎么办

    在IE的工具选项中把安全选项中的ActiveX控件一系列都改为启用即可.

  5. UVA 10168 Summation of Four Primes(数论)

    Summation of Four Primes Input: standard input Output: standard output Time Limit: 4 seconds Euler p ...

  6. Debian9.3安装NW360无线网卡驱动

    最近想把家里的一台老旧台式机利用起来,打算安装Debian9.3,下载ISO,用PowerISO写入到U盘,然后开始安装,过程基本顺利. 就是中间提示缺少“rtl_nic/rtl8105e-1.fw” ...

  7. 7.12归来赛_B

    Prime Judge 时间限制 1000 ms 内存限制 65536 KB 题目描写叙述 众所周知.假设一个正整数仅仅能被1和自身整除,那么该数被称为素数.题目的任务非常easy.就是判定一个数是否 ...

  8. Linux-进程基础

    计算机实际上可以做的事情实质上非常简单,比如计算两个数的和,再比如在内存中寻找到某个地址等等.这些最基础的计算机动作被称为指令 (instruction).所谓的程序(program),就是这样一系列 ...

  9. python selenium --unittest 框架

    转自:http://www.cnblogs.com/fnng/p/3300788.html 学习unittest 很好的一个切入点就是从selenium IDE 录制导出脚本.相信不少新手学习sele ...

  10. Chrome应用技巧之颜色拾取

    之前在Chrome应用店找了个插件实现拾色功能.并且很不理想.不知道是不是曾经Chrome自带的开发工具没提供到拾色功能还是我没发现.今天无意中发现Chomer自带的开发工具可拾色,请看以下的GIF动 ...