首页 > 编程语言 > 详细

python list 嵌套 dict 按照字典中的单个key进行单级排序 或 按照多个键进行多级排序

时间:2018-06-02 10:22:57      阅读:747      评论:0      收藏:0      [点我收藏+]
student = [{"no": 1,"score": 90},{"no": 2,"score": 90},{"no": 3,"score": 88},{"no": 4,"score": 92}]

# 单级排序,仅按照score排序
student_sort_1 = sorted(student, key=lambda e: e.__getitem__(score))

# 多级排序,先按照score,再按照no排序
student_sort_2 = sorted(student, key=lambda e:(e.__getitem__(score), e.__getitem__(no)))

 

python list 嵌套 dict 按照字典中的单个key进行单级排序 或 按照多个键进行多级排序

原文:https://www.cnblogs.com/lowmanisbusy/p/9124229.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!