Python sort方法
官方文档:
sort(*, key=None, reverse=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=None, reverse=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方法的更多相关文章
- python sort()方法
https://www.cnblogs.com/whaben/p/6495702.html https://www.cnblogs.com/sunny3312/p/6260472.html
- python sort和sorted的区别以及使用方法
iteralbe指的是能够一次返回它的一个成员的对象.iterable主要包括3类: 第一类是所有的序列类型,比如list(列表).str(字符串).tuple(元组). 第二类是一些非序列类型,比如 ...
- Python中的sort()方法使用基础
一.基本形式 sorted(iterable[, cmp[, key[, reverse]]]) iterable.sort(cmp[, key[, reverse]]) 参数解释: (1)itera ...
- python中List的sort方法的用法
python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. 关键字: python列表排序 python字典排序 sorted List的元素可以是各种东 ...
- 【转】python中List的sort方法(或者sorted内建函数)的用法
原始出处:http://gaopenghigh.iteye.com/blog/1483864 python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. ...
- python中的sort方法
Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不 ...
- python中的sort方法使用详解
Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不 ...
- Python 列表 sort() 方法
描述 Python 列表 sort() 方法对列表进行排序. 语法 sort() 方法语法: L.sort([key=None][,reverse=False]) 参数 key-- 可选参数, 如果指 ...
- python中sorted方法和列表的sort方法使用详解
一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不可修改的. 排序,数字.字符串按照ASCII,中文按照unicode从小到大排序 ...
随机推荐
- 浅谈使用git进行版本控制
小编在学习可视化的时候,接触到git,所以这里写一下关于GitHub的有关知识,写这个的目的还是巩固自己的学习,一方面可以提高自己,另一方面回头看一下,有什么更深层次的东西还可以再记录. 首先说一下版 ...
- php使用rc4加密算法
/** * rc4加密算法,解密方法直接再一次加密就是解密 * @param [type] $data 要加密的数据 * @param [type] $pwd 加密使用的key * @retur ...
- 【NOIP2015提高组】跳石头
https://www.luogu.org/problem/show?pid=2678 最小值最大问题,二分答案.每次检查是否能仅移走m块岩石使得所有跳跃距离均大于等于mid. #include &l ...
- 【NOIP2014提高组】飞扬的小鸟
https://www.luogu.org/problem/show?pid=1941 从某一点开始飞直到飞出地图最少点击屏幕的次数,显然只和该点的坐标唯一相关,没有后效性,考虑用DP解.令f(i,j ...
- Windows2000源代码 200+MB
全球最大的软件制造商微软2月12日警告公众称其一部分珍贵的Windows NT和Windows 2000操作系统源代码被泄漏到了一些在线文件共享网络中. 微软称被泄漏的代码只是整个程序的一小部分,但这 ...
- 数据库 E-R模型
数据库 E-R模型被定义被两种模型 "实体模型" AND "关系模型" 1.1 实体模型 如图:这是一个"项目表" Project ...
- pl/sql中if的用法
/*判断用户从键盘输入的数字1.如何使用if语句2.接收一个键盘输入(字符串)*/set serveroutput on--接收一个键盘输入--num:一个地址值:含义:在该地址上保存了输入的值acc ...
- Nginx软件部署配置过程
---恢复内容开始--- 注意:博主使用的系统为: [root@web01 ~]# uname -a Linux web01 2.6.32-696.el6.x86_64 #1 SMP Tue Mar ...
- TP3.2 图片上传及缩略图
基于TP自带的上传文件的类, Think/Upload.class.php 设置表单的enctype属性 下面是上传的具体方法 /** * 图片上传处理 * @param [String] $path ...
- macox下编译snappy静态库
源代码地址:https://github.com/google/snappy 下载 git clone https://github.com/google/snappy 编译 进入snappy源代码文 ...