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从小到大排序 ...
随机推荐
- 运行时动态库:not found 及介绍-linux的-Wl,-rpath命令
---此文章同步自我的CSDN博客--- 一.运行时动态库:not found 今天在使用linux编写c/c++程序时,需要用到第三方的动态库文件.刚开始编译完后,运行提示找不到动态库文件.我就 ...
- Python函数篇(2)-递归函数、匿名函数及高阶函数
1.全局变量和局部变量 一般定义在程序的最开始的变量称为函数变量,在子程序中定义的变量称为局部变量,可以简单的理解为,无缩进的为全局变量,有缩进的是局部变量,全局变量的作用域是整个程序,而局部变量的作 ...
- SpringCache与redis集成,优雅的缓存解决方案
缓存可以说是加速服务响应速度的一种非常有效并且简单的方式.在缓存领域,有很多知名的框架,如EhCache .Guava.HazelCast等.Redis作为key-value型数据库,由于他的这一特性 ...
- c# 初识WPF
WPF,全名是Windows Presentation Foundation,是微软在.net3.0 WinFX中提出的.WPF是对Direct3D的托管封装,它的图形表现依赖于显卡.当然,作为一种更 ...
- hdu1754 I Hate It (线段树 更新点的值)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- C++教程之autokeyword的使用
一.autokeyword的前世 从C语言開始,autokeyword就被当作是一个变量的存储类型修饰符,表示自己主动变量(局部变量).它不能被单独使用,否则编译器会给出警告. #include &l ...
- Comparable和Comparator的差别
原文地址:http://leihuang.org/2014/11/16/Comparable-Vs-Comparator/ Comparable和Comparator都是用来实现集合中元素的比較.排序 ...
- 有关JS控制时间的几个小Demo
一.Document自带的定时和延时方法: 循环运行:var timeid = window.setInterval("方法名或方法"."延时");windo ...
- 以流方式读写文件:文件菜单打开一个文件,文件内容显示在RichTexBox中,执行复制、剪切、粘贴后,通过文件菜单可以保存修改后的文件。
MainWindow.xaml文件 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&q ...
- MyBatis_多查询条件问题
一.多查询条件无法整体接收问题的解决 在实际工作中,表单中所给出的查询条件有时是无法将其封装成一个对象,即查询方法只能携带多个参数,而不能携带将这多个参数进行封装的一个对象.对于这个问题,有两种解决方 ...