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参数没有深入研究,总以为就是以 ...
随机推荐
- java 中PriorityQueue优先级队列使用方法
1.前言 优先级队列是不同于先进先出队列的另一种队列.每次从队列中取出的是具有最高优先权的元素. PriorityQueue是从JDK1.5开始提供的新的数据结构接口. 如果想实现按照自己的意愿进行优 ...
- C#/Sqlite-单机Window 程序 sqlite 数据库实现
数据库分析和选择 Excel 文件 做数据源 限制性比较强,且不适合查询,分析 等操作 Access 数据库 Access 管理数据界面和功能不强 mysql 和sql server 功能满足,但需要 ...
- php.in
[PHP] ;;;;;;;;;;; ; WARNING ; ;;;;;;;;;;; ; This is the default settings file for new PHP installati ...
- 一条长为L的绳子,一面靠墙,另外三边组成矩形,问此矩形最大面积能是多少?
靠墙的两边设为x,墙的对边设为y,有2x+y=L; 则y=L-2x, 矩形面积函数为xy=x(L-2x)=-2x2+xL,即f(x)=-2x2+xL 这时就是求二次函数的极值问题了. 按二次函数y=a ...
- iOS key value coding kvc在接收json数据与 model封装中的使用
iOS key value coding kvc在接收json数据与 model封装中的使用 使用 kvc 能够极大的简化代码工作,及以后的接口维护工作: 1:先创建MovieModel类.h和 . ...
- vue - src for components || router(index.js)
描述:重新编写一个组件 1.1 编写一个PrintName.vue <!--这里是模板 --> <template> <div class="hello&quo ...
- nginx做正向代理(Centos7,支持http和https)
默认的情况下,使用nginx做正向代理可以解析http请求, 对于诸如baidu.com这样的https请求,nginx默认并不支持,不过我们可以借助第三方模块来实现. 1.先说默认情况下的代理配置 ...
- how to remove untagged / none images
docker rmi $(docker images -a| grep "^<none>" | awk '{print $"3"}')
- Drupal与其它开源系统的整合
网上看到一篇介绍Drupal与phpbb整合的文章.浏览了一下,真心地不错.于是就想将与整合有关的文章做一个汇总,以备不时之需: Drupal7整合PHPBB论坛 Drupal 7 整合 Vanill ...
- 单元测试JUnit 4 (一)——keeps the bar green to keeps the code clean
1. 导读 Junit是一个可编写重复测试的简单框架,是基于Xunit架构的单元测试框架的实例.Junit4最大的改进是大量使用注解(元数据),很多实际执行过程都在Junit的后台做完了,而且写tes ...