首页 > 编程语言 > 详细

字典按key值排序

时间:2020-02-21 14:36:15      阅读:51      评论:0      收藏:0      [点我收藏+]

 

data_dic = {20190105: [{name:bbb,value: 222}, {name:aaa, value: 111}],
            20190101: [{name:bbb,value: 333}, {name:aaa, value: 555}]}
# 列表按 name值字段排序
for key, value in data_dic.items():
    value_temp = sorted(value, key=lambda my_sort: my_sort[name])
    data_dic[key] = value_temp
# 字典按key排序
data = dict(sorted(data_dic.items(), key=lambda x: x[0]))
print(data)
# 输出结果 {20190101: [{name: aaa, value: 555}, {name: bbb, value: 333}], 20190105: [{name: aaa, value: 111}, {name: bbb, value: 222}]}

 

字典按key值排序

原文:https://www.cnblogs.com/snailgirl/p/12341028.html

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