首页 > 解决方案 > 对嵌套字典列表进行排序

问题描述

我有一个大型嵌套字典,需要从中排序和提取数据,但我不知道如何解决这个问题。这是dict表格:

mydict = {Person 1 : [{'2017-06-02': {'Country A': {'City 1': {'Population': '0', 'Temp': '10'}}}, '2016-06-02': {'Country B': {'City 2' :{'Population':'1'}}}]

我可以使用什么方法按日期对列表进行排序并在之后对其进行迭代?

注意:实际的字典有更多的条目

标签: pythonsortingdictionary

解决方案


不确定这是否是您要搜索的内容

mydict = {'Person 1' : [{'2017-06-02': {'Country A': {'City 1': {'Population': '0', 'Temp': '10'}}}, '2015-06-02': {'Country A': {'City 1': {'Population': '0', 'Temp': '10'}}}}]}

od = collections.OrderedDict(sorted(mydict["Person 1"][0].items()))

推荐阅读