【说站】python sorted()函数的参数用法
Python 函数 用法 参数 sorted
2023-06-13 09:13:22 时间
python sorted()函数的参数用法
概念
1、对可迭代对象中的数据进行排序,返回一个新的列表。指定 key 排序需要用到 lambda 表达式。
语法
sorted(iterable, key=None, reverse=False)
参数
iterable – 可迭代对象。
key – 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
reverse – 排序规则,reverse = True 降序 , reverse = False 升序(默认)。
2、通过参数reverse=True指定倒序,参数 key指定排序时所使用的字段。
(1)通过参数 reverse=True 指定倒序:
>>> numbers = (4, 5, 2, 8, 9, 1, 0)
>>> sorted(numbers, reverse=True)
[9, 8, 5, 4, 2, 1, 0]
(2)通过参数 key 指定排序时所使用的字段:
>>> codes = [(‘上海’, ‘021’), (‘北京’, ‘010’), (‘成都’, ‘028’), (‘广州’, ‘020’)]
>>> sorted(codes, key=lambda x: x[1])
[(‘北京’, ‘010’), (‘广州’, ‘020’), (‘上海’, ‘021’), (‘成都’, ‘028’)]
以上就是python sorted()函数参数用法的介绍,希望对大家有所帮助。
相关文章
- Python 编程 | 连载 14 - Python 的函数
- python中关于round函数的小坑「建议收藏」
- python实现求和,求平均值——函数
- nonzero函数_python 类方法
- python更新pip失败_findwindow函数
- python setattr函数_Python内置函数(53)——setattr
- 【说站】python pprint.pformat()函数的使用
- 【说站】python中rindex函数是什么
- 【说站】python中lambdas匿名函数的用法
- Python标识符的命名规则,下列哪些是对的?_python标识符不能使用关键字
- python中drop用法_python compile函数
- python中main的含义及用法_python main函数有什么用
- python中的imread函数_python open函数参数
- python中函数的返回值详解
- Python print()函数高级用法
- Python zip函数及用法
- Python os.path模块常见函数用法(实例+详细注释)
- Python时间,日期,时间戳之间转换,时间转换时间戳,Python时间戳转换时间,Python时间转换时间戳详解编程语言
- Python lambda函数(匿名函数)的定义
- 在Linux上学习Python——你的编程之路(linux学python)
- Python轻松连接SQL Server数据库(python连接sqlserver)
- python从MSSQL到Python:一段跨语言的旅程(mssql除以)