英文文档:

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. JAVA设计模式--装饰器模式

    装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...

  2. Starting a Gradle Daemon, 5 busy and 1 incompatible and 1 stopped Daemons could not be reused, use --status for details FAILURE: Build failed with an exception. * What went wrong: Could not dispatch

    执行gradle build出的问题,查看hs_err_pid11064.log日志文件发现,是电脑的RAM不足导致

  3. ServiceFabric极简文档-0. ServiceFabric简介

    前言: 最近ServiceFabric开源了,大家热情都比较高,官方文档大而全,但快速入手不容易找到头绪.发几篇极简的文档,跟大家分享一下,顺便为Ray的ServiceFabric部署做一下铺垫.因为 ...

  4. 云主机和vps的区别

    云主机和vps的区别   近年来,IT行业开始热衷于各种云概念,而云主机就是其中之一,并且有越来越热之势.对普通用户而言,可能不太清楚云主机和VPS的区别,下面我们就来说说云主机和VPS到底有什么不同 ...

  5. Go a lot of way but I go back to the original point

    I try a lot of blog platform and even construct my blog website. But I have to say I just want to ha ...

  6. python中干掉tornado的连接失败日志

    用了tornado真的是比较舒服,很多事都为你做好了. 但也有不令人满意的地方--对于我这个洁癖来说,自动给我的控制台打印不受我控制的信息是不能忍受的. 连接到一个新的地方,如果失败,tornado会 ...

  7. Java生成图片验证码

    在日常我们在登录或者注册的时候,网页上会出现验证码让我们填写,其实利用jdk提供给我们的工具类完全可以模拟出来一个生成验证码图片的功能. package util; import javax.imag ...

  8. Maven-06: 插件的内置绑定

    Maven的生命周期与插件相互绑定,用以完成实际的构建任务.具体而言,是生命周期的阶段与插件的目标相互绑定,以完成某个具体的构建任务.例如项目编译这一任务,它对应default生命周期的compile ...

  9. Adriod与HTML+JS的交互

    本篇主要实现的功能点: Android 调用HTML中的javascript脚本 HTML中的javascript脚本调用Android本地代码 Android 调用HTML中的javascript脚 ...

  10. 很全的atom问题解决方案

    atom插件 http://blog.csdn.net/qq_30100043/article/details/53558381 atom社区 https://atom-china.org/