官方文档:

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. Spring之bean一基础

    在前面得博客依赖注入与控制反转中演示了应用spring实现ioc,在ApplicationContext.xml中有bean的配置,里面只是bean简单的应用.这篇主要是进一步学习使用bean. 一. ...

  2. 前端CSS的工程化——掌握Sass这四大特性就够了

    初遇 CSS,一见倾心 记得刚遇见css的时候,咱像是见了美人儿一样,简直是爱不释手啊,简简单单写几行算不上代码的代码,就能做出这么漂亮的东西,这也成了咱前端之路的最初动力. 然而,随着项目体量和页面 ...

  3. Python函数篇(4)之迭代器与生成器

    1.文件操作的"b模式"(补充) 在上一篇文章中,我在最后一部分写了文件处理的一些方法,但是觉得还是有必要再提一下如下的内容: 像rb.wb.ab这种模式,是以字节的形式操作,需要 ...

  4. 网页设计——4.html基本标签链接,图片,表格

    今天主要认识一个有趣的html标签 1.超链接标签a 基本结构:<a href="url" >内容 </a> url就是网页地址. 但直接用这种超链接,会自 ...

  5. python3.6 urllib.request库实现简单的网络爬虫、下载图片

    #更新日志:#0418 爬取页面商品URL#0421 更新 添加爬取下载页面图片功能#0423 更新 添加发送邮件功能# 优化 爬虫异常处理.错误页面及空页面处理# 优化 爬虫关键字黑名单.白名单,提 ...

  6. JavaWeb学习总结(二)——Tomcat服务器学习和使用(一)(转)

    转载自 http://www.cnblogs.com/xdp-gacl/p/3734395.html 一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的serve ...

  7. JavaWeb学习总结(一)——JavaWeb开发入门(转)

    转载自 http://www.cnblogs.com/xdp-gacl/p/3729033.html 一.基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示In ...

  8. JDBC中的Statement和PreparedStatement的差别

    以Oracle为例吧 Statement为一条Sql语句生成运行计划, 假设要运行两条sql语句 select colume from table where colume=1; select col ...

  9. Weka学习 -- StringToWordVector 源代码学习(1)

    代码整个运行流程 參数设置 input数据,设置数据格式 batchFinished(),处理数据(Tokenzier,Stemming,Stopwords) determineDictionary( ...

  10. 【Android】定位与解决anr错误记录

    问题描写叙述 cocos2d-x游戏项目androidproject接入sdk.支付成功后,java代码回调lua方法.产生了anr. 怎样定位anr? watermark/2/text/aHR0cD ...