Python内置函数(59)——sorted
英文文档:
sorted
(iterable[, key][, reverse])
Return a new sorted list from the items in iterable.
Has two optional arguments which must be specified as keyword arguments.
key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower
. The default value is None
(compare the elements directly).
reverse is a boolean value. If set to True
, then the list elements are sorted as if each comparison were reversed.
Use functools.cmp_to_key()
to convert an old-style cmp function to a key function.
The built-in sorted()
function 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).
说明:
1. 函数功能对一个可迭代对象进行排序,返回一个排序后列表。
- >>> a = sorted('dcabegf')
- >>> a # 返回结果是列表
- ['a', 'b', 'c', 'd', 'e', 'f', 'g']
2. 函数调用时可以提供一个可选的命名参数key,它是一个方法,默认值是None,用来指定具体排序的算法;函数对可迭代对象每个元素使用key算法后再排序,返回的任然是可迭代对象中的元素。
- >>> a = ['a','b','d','c','B','A']
- >>> a
- ['a', 'b', 'd', 'c', 'B', 'A']
- >>> sorted(a) # 默认按字符ascii码排序
- ['A', 'B', 'a', 'b', 'c', 'd']
- >>> sorted(a,key = str.lower) # 转换成小写后再排序,'a'和'A'值一样,'b'和'B'值一样
- ['a', 'A', 'b', 'B', 'c', 'd']
3. 函数调用时可以提供一个可选的命名参数reverse,它的默认值是False,用来排序结果是否倒转。
- >>> a = sorted('dcabegf')
- >>> a
- ['a', 'b', 'c', 'd', 'e', 'f', 'g']
- >>> a = sorted('dcabegf',reverse = True) # 排序结果倒置
- >>> a
- ['g', 'f', 'e', 'd', 'c', 'b', 'a']
Python内置函数(59)——sorted的更多相关文章
- Python 内置函数 -- zip(), sorted(), filter()和map()
内置函数1. zip() 打包(木桶效应)描述: zip() 函数用于将可迭代的对象作为参数, 将对象中对应的元素打包成一个个元组, 然后返回由这些元组组成的列表语法: zip([iterable, ...
- python内置函数:sorted中的参数key
x.sort和sorted函数中参数key的使用 介绍 python中,列表自带了排序函数sort >>> l = [1, 3, 2] >>> l.sort() & ...
- Python内置函数(59)——open
英文文档: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, ope ...
- Python内置函数(37)——sorted
英文文档: sorted(iterable[, key][, reverse]) Return a new sorted list from the items in iterable. Has tw ...
- Python内置函数之sorted()
sorted(iterable,*,key=None,reverse=False) 对可迭代对象进行排序,默认ASCII进行排序. 例子: sorted(iterable,*,key=None,rev ...
- Python 内置函数sorted()在高级用法
对于Python内置函数sorted(),先拿来跟list(列表)中的成员函数list.sort()进行下对比.在本质上,list的排序和内建函数sorted的排序是差不多的,连参数都基本上是一样的. ...
- python内置函数sorted()及sort() 函数用法和区别
python内置函数sorted(),sort()都有排序的意思,但是两者有本质的区别,sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作,list 的 sort ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
随机推荐
- 项目管理目标:添加人员并向其分配任务 - Project
已剪辑自: https://support.office.com/zh-cn/article/%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86%E7%9B%AE%E6%A0%8 ...
- 台达PLC modbus 不支持04功能码
如果有04功能码的通讯,需要使用RS指令
- Docker 学习4 Docker容器虚拟化网络概述
一.docker 虚拟化网络概述 1.OVS: OpenVSwitch,不仅能模拟二层网络,还能模拟三层网络,或者VLAN,VXLAN,流控 SDN软件定义网络技术等. 2.overlay netwo ...
- Thread类和Runnable接口实现多线程--2019-4-18
1.通过Thread实现 public class TestThread extends Thread{ public TestThread(String name) { super(name); } ...
- python基础知识练习题(一)
1.执行Python脚本的两种方式:WIN+R,cmd,命令行窗口输入:python 进入python模式输入命令行直接执行命令:编写以.py结尾的文件,写入命令行,然后运行python.exe打开 ...
- xxl-job调度中心配置以及常见错误
项目结构图 启动步骤: 1.检查 /xxl-job/xxl-job-admin/src/main/resources/xxl-job-admin.properties 下的JDBC链接.登录账号. 2 ...
- 短网址API
http://tao.tf/open/ API简介 API允许第三方自由调用URL缩短,基于text/json/jsonp/js模式,支持post.get提交. 支持缩短网址: 淘宝网(*.taoba ...
- 2019年3月2日-小雨.md
2019年3月2日, 星期六 开学已经一周了,时间好像限制了自己进步的脚步,一个人的精力有限,想做好方方面面实在是太难了,有很多事儿最后都没做的完美.相反,自己应该放下繁琐的包袱,简简单单的干一件事儿 ...
- Android应用程序如何使用Internet资源?
思路:连接Internet资源-->分析XML资源-->使用Download Manager下载文件 Android的Internet连接模型和用于分析Internet数据源的Java技术 ...
- web基础要点记录
最近公司项目做完了,不怎么忙,翻看了一些基础的资料,文章.就做了个简单的记录. 1.Chrome 中文界面下默认会将小于 12px 的文本强制按照 12px 显示, 可通过加入 CSS 属性 -we ...