您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python-按多个属性对列表进行排序?

Python-按多个属性对列表进行排序?

键可以是返回元组函数

s = sorted(s, key = lambda x: (x[1], x[2]))

或者,你可以使用来实现相同的效果itemgetter(速度更快,并且避免了Python函数调用):

import operator
s = sorted(s, key = operator.itemgetter(1, 2))

并请注意,你可以在此处使用sort而不是使用sorted,然后重新分配:

s.sort(key = operator.itemgetter(1, 2))
python 2022/1/1 18:24:10 有533人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶