博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python sort方法
阅读量:6475 次
发布时间:2019-06-23

本文共 2535 字,大约阅读时间需要 8 分钟。

官方文档:

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).

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

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  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  to explicitly request a new sorted list instance).

The  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  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 "
", line 1, in
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]>>>

 

转载于:https://www.cnblogs.com/huahuayu/p/8179856.html

你可能感兴趣的文章
DNS为什么通常都会设置为14.114.114.114
查看>>
Sqoop架构(四)
查看>>
golang copy函数
查看>>
《你有多少问题要请示》精华集粹
查看>>
leveldb学习:DBimpl
查看>>
打印图片
查看>>
SHOW CREATE DATABASE Syntax
查看>>
rsync常见问题及解决办法
查看>>
MySQL日期 专题
查看>>
C#中禁止程序多开
查看>>
分布式缓存Redis使用以及原理
查看>>
Activity竟然有两个onCreate方法,可别用错了
查看>>
Linux经常使用命令(十六) - whereis
查看>>
插件编译 版本问题
查看>>
android中TextView的阴影设置
查看>>
core dump相关
查看>>
Linux五种IO模型
查看>>
Bootstrap技术: 模式对话框的使用
查看>>
小知识,用myeclipes找jar
查看>>
in-list expansion
查看>>