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‘}]}
原文地址:https://www.cnblogs.com/snailgirl/p/12341028.html
时间: 2024-11-08 07:30:29