官方文档:

sort(*key=Nonereverse=False)

This method sorts the list in place, using only < comparisons between items. Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state).

sort() accepts two arguments that can only be passed by keyword (keyword-only arguments):

key specifies a function of one argument that is used to extract a comparison key from each list element (for example, key=str.lower). The key corresponding to each item in the list is calculated once and then used for the entire sorting process. The default value of None means that list items are sorted directly without calculating a separate key value.

The functools.cmp_to_key() utility is available to convert a 2.x style cmp function to a key function.

reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

This method modifies the sequence in place for economy of space when sorting a large sequence. To remind users that it operates by side effect, it does not return the sorted sequence (use sorted() to explicitly request a new sorted list instance).

The sort() method is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade).

CPython implementation detail: While a list is being sorted, the effect of attempting to mutate, or even inspect, the list is undefined. The C implementation of Python makes the list appear empty for the duration, and raises ValueError if it can detect that the list has been mutated during a sort.

sort()只能对纯数字或字母进行排序, 否则报错:

>>> alist
[1, 3, 4, 7, 8, 9]
>>> alist.append('a')
>>> alist.sort()
Traceback (most recent call last):
File "<pyshell#83>", line 1, in <module>
alist.sort()
TypeError: '<' not supported between instances of 'str' and 'int'
>>>

sort()可以不带参数或最多带两个关键字参数,默认key=Nonereverse=False, 默认升序排列:

>>> alist = [1,4,2,7,9,3]
>>> alist.sort()
>>> alist
[1, 2, 3, 4, 7, 9]
>>>

指定reverse=True时降序排列:

>>> alist = [1,4,2,7,9,3]
>>> alist.sort(reverse=True)
>>> alist
[9, 7, 4, 3, 2, 1]
>>>

如果想保持原list不变,拷贝一个新list出来排序:

>>> alist = [1,4,2,7,9,3]
>>> blist = alist[:] #不能写blist = alist,这样写是让blist指向alist同一内存空间
>>> blist.sort()
>>> alist
[1, 4, 2, 7, 9, 3]
>>> blist
[1, 2, 3, 4, 7, 9]
>>>

或者用sorted()方法:

>>> alist = [1,4,2,7,9,3]
>>> blist = sorted(alist)
>>> alist
[1, 4, 2, 7, 9, 3]
>>> blist
[1, 2, 3, 4, 7, 9]
>>>

Python sort方法的更多相关文章

  1. python sort()方法

    https://www.cnblogs.com/whaben/p/6495702.html https://www.cnblogs.com/sunny3312/p/6260472.html

  2. python sort和sorted的区别以及使用方法

    iteralbe指的是能够一次返回它的一个成员的对象.iterable主要包括3类: 第一类是所有的序列类型,比如list(列表).str(字符串).tuple(元组). 第二类是一些非序列类型,比如 ...

  3. Python中的sort()方法使用基础

    一.基本形式 sorted(iterable[, cmp[, key[, reverse]]]) iterable.sort(cmp[, key[, reverse]]) 参数解释: (1)itera ...

  4. python中List的sort方法的用法

    python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. 关键字: python列表排序 python字典排序 sorted List的元素可以是各种东 ...

  5. 【转】python中List的sort方法(或者sorted内建函数)的用法

    原始出处:http://gaopenghigh.iteye.com/blog/1483864 python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. ...

  6. python中的sort方法

    Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不 ...

  7. python中的sort方法使用详解

    Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不 ...

  8. Python 列表 sort() 方法

    描述 Python 列表 sort() 方法对列表进行排序. 语法 sort() 方法语法: L.sort([key=None][,reverse=False]) 参数 key-- 可选参数, 如果指 ...

  9. python中sorted方法和列表的sort方法使用详解

    一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不可修改的. 排序,数字.字符串按照ASCII,中文按照unicode从小到大排序 ...

随机推荐

  1. Nginx的知识分享,继续上次的分享

    5. Nginx配置文件精讲二 #这里为后端服务器wugk应用集群配置,根据后端实际情况修改即可,tdt_wugk为负载均衡名称,可以任意指定 #但必须跟vhosts.conf虚拟主机的pass段一致 ...

  2. tomcat警告setting property 'debug' to '0' did not find a matching property

    在使用tomcat6.0版本结合myeclipse进行java web项目,运行程序显示setting property 'debug' to '0' did not find a matching ...

  3. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

  4. CDH集群搭建部署

    1. 硬件准备     使用了五台机器,其中两台8c16g,三台4c8g.一台4c8g用于搭建cmServer和NFS服务端,另外4台作为cloudera-manager agent部署CDH集群. ...

  5. mybatis延迟加载一对多

    1.实体类 package cn.bdqn.bean; import java.util.Set; /** *国家的实体类 */ public class Country { private Inte ...

  6. Python 开发与接口测试学习笔记

    这是我跟着虫师学习中积累下来的学习笔记,写得比较简单,适合想学习Python开发与接口测试的初学者学习. 一.开发投票系统 1.参考官网文档,创建投票系统. https://docs.djangopr ...

  7. Usaco 2006Nov Round Numbers

    题意:定义Round Number为二进制表示下0的个数大于等于1的个数的数.求[l,r]中有多少圆环数 我们把二进制位用一颗01二叉树表示,如下: 我们依据二进制位来遍历这颗线段树,如果当前高度对应 ...

  8. 小白的Python之路 day1 Python3的bytes/str之别

    原文:The bytes/str dichotomy in Python 3 Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分.文本总是Unicode,由str类型表示,二 ...

  9. coursera普林斯顿算法课part1里Programming Assignment 2最后的extra challenge

    先附上challenge要求: 博主最近在刷coursera普林斯顿大学算法课part1部分的作业,Programming Assignment2最后的这个extra challenge当初想了一段时 ...

  10. Unity3D中通过Animator动画状态机获取任意animation clip的准确播放持续时长

    Unity3d 4及之前的版本中动画的播放用的animation,可直接获取其播放持续长度.但5.x及以后的版本中都是用animator来播放动画了. https://docs.unity3d.com ...