英文文档:

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的更多相关文章

  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)——open

    英文文档: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, ope ...

  4. Python内置函数(37)——sorted

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

  5. Python内置函数之sorted()

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

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

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

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

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

  8. Python | 内置函数(BIF)

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

  9. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

随机推荐

  1. Nginx 如何设置反向代理 多服务器,配置区分开来,单独文件保存单个服务器 server 主机名配置,通过 include 实现

    samcao 关注 2015.06.15 10:08* 字数 0 阅读 408评论 0喜欢 0   网络结构如上图.可能你只有一个公网的Ip地址. 但是您的内网有个网站需要映射至外网.而又不想添加其它 ...

  2. HTTP/1.1 chunked 解码

    0.简介 1.定义 RFC定义 https://tools.ietf.org/html/rfc2616#section-3.6.1 Chunked-Body = *chunk last-chunk t ...

  3. JS与CSS那些特别小的知识点区别

    1:target与currentTarget的区别 currentTarget指向的事件绑定的元素,target指向的是你点击的元素 2:attr与jprop在jQuery在API当中的区别 2.1: ...

  4. native的详细用法

    目录 1.JNI:Java Native Interface 3.用C语言编写程序本地方法 一.编写带有 native 声明的方法的java类 二.使用 javac 命令编译所编写的java类,生成. ...

  5. MongoDb安装和快速入门

    1.Mongodb安装 2.mongodb的增删改查 3.MongoDB数据类型 4.Mongodb $关键字 $修改器 5.MongoDB 之 "$" 的奇妙用法 6.Mongo ...

  6. <玩转Django2.0>读书笔记:表单

    1. 表单字段 参考: 官方文档 Django表单字段汇总 2. 表单代码示例(forms.Form) # form.py代码 # 获取数据库数据 choices_list = [(i+1,v['ty ...

  7. 第六章 对象-javaScript权威指南第六版(四)

    6.6 属性getter和setter 对象属性是由名字.值和一组特性(attribute)构成的. getter和setter定义的属性称做"存取器属性"(accessor pr ...

  8. c#坐标系互相转换

    转自群友的博客:https://www.xiaofengyu.com/?p=108 群友的github地址:https://github.com/jfwangncs/GPSConvert  各种坐标系 ...

  9. 分布式、集群、微服务、SOA 之间的区别

    分布式:不同模块部署在不同服务器上 作用:分布式解决网站高并发带来问题 集群:多台服务器部署相同应用构成一个集群 作用:通过负载均衡设备共同对外提供服务 SOA:Service Oriented Ar ...

  10. 上传插件webupload之调用拍照兼容问题

    在项目中,移动端用到了webupload插件来实现上传功能(我觉得这个插件挺好用的,所以无论pc还是移动端我都使用了这个插件来做上传功能) 在移动端要调起拍照功能,实现上传,须得在webuploade ...