这两种方法使用起来差不多,以第一种为例进行讲解:
从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:(推荐1)
>>>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:(推荐2)
>>>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:(推荐3)
>>> 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:(推荐4)
>>> 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是的比较从左到右之一比较的,比较完第一个,如果
相等,比较第二个……

以上内容整理自:http://blog.chinaunix.net/u2/71210/showart_1888878.html

Python: How to Sort a List相关推荐

  1. python中的sort,sorted

    python中的sort,sorted 文章目录 python中的sort,sorted 两者区别 用法: 用法示例 对字典排序: 对列表排序: 两者区别 sorted有返回值,而sort没有返回值. ...

  2. python排序之sort和sorted

    python排序之sort和sorted 在使用python的时候很多时候需要用到排序操作,自己挨个去排太麻烦,而且效率也比较低下.因此学会使用Python自带的排序函数就显得尤为必要了. pytho ...

  3. python中的sort排序加换行_python中sort()排序的方法

    python中sort()排序的方法 发布时间:2020-09-01 10:57:52 来源:亿速云 阅读:110 作者:小新 这篇文章主要介绍了python中sort()排序的方法,具有一定借鉴价值 ...

  4. Python:实现natural sort自然排序算法(附完整源码)

    Python:实现natural sort自然排序算法 from __future__ import annotationsimport re msd radix sortdef natural_so ...

  5. Python中的sort()使用方法

    Python中的sort()方法使用基础 一.基本形式 sorted(iterable[, cmp[, key[, reverse]]])iterable.sort(cmp[, key[, rever ...

  6. Python:实现comb sort梳状排序算法(附完整源码)

    Python:实现comb sort梳状排序算法 def comb_sort(data: list) -> list:shrink_factor = 1.3gap = len(data)comp ...

  7. Python:实现pancake sort煎饼排序算法(附完整源码)

    Python:实现pancake sort煎饼排序算法 def pancake_sort(arr):cur = len(arr)while cur > 1:# Find the maximum ...

  8. sort函数pythonreverse_Python基础 7 ---- Python内置sort和sorted函数

    1 Python对数据的排序有两种方法,一种是容器内置的sort函数,另外一种利用sorted函数 2 对于sort函数我们不再进行讨论,只要研究一下sorted函数 3 sorted函数的原形sor ...

  9. python 排序函数 sort sorted 简介

    sort() 是Python列表的一个内置的排序方法,list.sort() 方法排序时直接修改原列表,返回None: sort() 是Python内置的一个排序函数,它会从一个迭代器返回一个排好序的 ...

最新文章

  1. Python源码学习:多线程实现机制
  2. “学在清华”清华大学本科教育主题展在校史馆开展
  3. 微服务限流Sentinel讲解(二)
  4. 判断是不是链接 正则_Python 正则表达式 保姆级教程,小学生都看得懂!!
  5. golang协程特点
  6. scrapy mysql 报错_scrapy爬数据存mysql报错
  7. OGNL中#和%的用法
  8. Hbase 01_初学必知
  9. Event Handling Guide for iOS——由触摸事件传递想到的
  10. MACAPP中引入ffmpeg库完成具体功能
  11. ASP.NET AJAX的客户端框架是鸡肋?
  12. Mixly-RFID智能门禁
  13. RocketMQ创建topic流程解析
  14. java 获取流 丢失_java文件流数据丢失问题
  15. c语言谢旻吕俊张军强答案,吕俊|
  16. java qq开发_Ubuntu用户的福音:基于Java开发的开源QQ客户端iQQ
  17. PhotoScan Google照片扫描仪,让手机替代扫描仪
  18. 快速掌握的微信运营技巧
  19. 流利阅读 2019.3.5 Your friends’ social media posts are making you spend more money, researchers say
  20. 基于SSM婚恋网交友平台

热门文章

  1. 保障IDC安全:分布式HIDS集群架构设计
  2. SQLServer 大小写敏感配置
  3. 太白教你学python---博客分类目录
  4. 设计模式(10)-装饰模式详解(易懂)
  5. stdio.h iostream.h iostream 三者
  6. ImportError: No module named _sqlite3 报错解决方法
  7. 是什么优化让 .NET Core 性能飙升?
  8. sqlserver阻止保存要求重新建立表的更改
  9. iOS开发UI基础—手写控件,frame,center和bounds属性
  10. Make Games with Python Pygame (2)