英文文档:

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内置函数(37)——sorted的更多相关文章

  1. Python 内置函数 -- zip(), sorted(), filter()和map()

    内置函数1. zip() 打包(木桶效应)描述: zip() 函数用于将可迭代的对象作为参数, 将对象中对应的元素打包成一个个元组, 然后返回由这些元组组成的列表语法: zip([iterable, ...

  2. python内置函数:sorted中的参数key

    x.sort和sorted函数中参数key的使用 介绍 python中,列表自带了排序函数sort >>> l = [1, 3, 2] >>> l.sort() & ...

  3. Python内置函数(59)——sorted

    英文文档: sorted(iterable[, key][, reverse]) Return a new sorted list from the items in iterable. Has tw ...

  4. Python内置函数之sorted()

    sorted(iterable,*,key=None,reverse=False) 对可迭代对象进行排序,默认ASCII进行排序. 例子: sorted(iterable,*,key=None,rev ...

  5. Python内置函数(37)——len

    英文文档: len(s) Return the length (the number of items) of an object. The argument may be a sequence (s ...

  6. Python 内置函数sorted()在高级用法

    对于Python内置函数sorted(),先拿来跟list(列表)中的成员函数list.sort()进行下对比.在本质上,list的排序和内建函数sorted的排序是差不多的,连参数都基本上是一样的. ...

  7. python内置函数sorted()及sort() 函数用法和区别

    python内置函数sorted(),sort()都有排序的意思,但是两者有本质的区别,sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作,list 的 sort ...

  8. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

  9. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

随机推荐

  1. JVM堆外内存随笔

    一 JVM堆外内存 1)java与io(file,socket)的操作都需要堆外内存与jvm内存进行互相拷贝,因为操作系统是不懂jvm的内存结构的(jvm的内存结构是自管理的),所以堆外内存存放的是操 ...

  2. assert断言检测

    assert 是宏,非函数,包含在assert.h 头文件中. 如果其后面括号里的值为假,则程序终止运行,并提示出错.这个 宏只在 Debug 版本上起作用,而在 Release 版本被编译器完全优化 ...

  3. 关于eclipse安装Genymotion插件的方法

    其实Genymotion的安装方法也有两种:在线安装和离线安装,不过推荐使用在线安装,这个更快.这里我只说在线安装的方法. 打开eclipse,点击help-install new software ...

  4. Unity服务端链接MySQL数据库

    链接数据库,可以分为以下几个流程: 链接MySQL(数据库地址.端口.用户名.密码) 选择数据库 执行sql语句 关闭数据库 转换为代码如下: using System; using MySql.Da ...

  5. 使用c#对MongoDB进行查询(1)

    1.BsonDocument对象 在MongoDB.Bson命名空间下存在一个BsonDocument类,它是MongoDB的文档对象,代表着MongoDB中不规则数据一条条实体模型.可以使用Bson ...

  6. Filecoin协议(挖矿)

    Filecoin协议是如何运作的? 用户如何使用Filecoin系统来查询,存储数据? 存储矿工如何挖矿,如何获取FIL代币? 检索矿工如何挖矿,获取FIL代币? 本文主要回答上面的这些问题!!! 首 ...

  7. jQuery 3.0最终版发布,十大新特性眼前一亮

    jQuery 3.0在日前发布了最终的全新版本.从2014年10月,jQuery团队对这个主要大版本进行维护开始,web开发者社区便一直在期待着这一刻的到来,终于在2016年6月他们迎来了这一个最终板 ...

  8. Xss问题解决方案

    xss跨站脚本攻击问题最主要是呈现在html页面的脚本被执行导致的结果,可分为两个方便作屏蔽 后台屏蔽 在前端上传的各个参数后,对其进行转义后再保存至数据库,属于暴力式转义,一般不建议.下面是写的例子 ...

  9. trigger回调方法的实现

    用传参实现trigger的回调: 点击btn1触发btn2的click事件并执行trigger中传入的回调方法 <body> <input type="button&quo ...

  10. Mybatis转义字符

    Mybatis的sql语句中需要用到'>'或者'<'时,不能直接使用. < < 小于号 > > 大于号 & & 和 &apos; ' 单引号 ...