Python3排序 sorted(key=lambda)
发布时间:2024-11-13 02:44
当待排序列表的元素由多字段构成时,我们可以通过sorted(iterable,key,reverse)的参数key来制定我们根据那个字段对列表元素进行排序。
key=lambda 元素: 元素[字段索引]
例如:想对元素第二个字段排序,则
key=lambda y: y[1] 备注:这里y可以是任意字母,等同key=lambda x: x[1]
看几个简单的例子。
items.sort(key=lambda x:x[1],reverse=True)
print(items)
原列表:
[('while', 1), ('there', 2), ('is', 2), ('life', 1), ('hope', 1)]
输出:
[('there', 2), ('is', 2), ('while', 1), ('life', 1), ('hope', 1)]
网址:Python3排序 sorted(key=lambda) https://www.yuejiaxmz.com/news/view/63169
下一篇:用python写一个股票提醒、并
相关内容
Python实现简便算法提升拼音输入法准确率与效率Multiple entries with same key: 1=1 and 1=1
Python中的遇到的错误(持续更新)
Python 双向队列Deque
【微信小程序】基础篇
化妆品 组织者 排序 游戏
Win11 全系通用激活码
小常识
达梦数据库
python中pow