Sorting a dictionary (with date keys) in Python

后端 未结 6 868
轮回少年
轮回少年 2020-12-29 23:40

I have a dictionary. The keys are dates (datetime). I need to sort the dictionary so that the values in the dictionary are sorted by date - so that by iterating through the

6条回答
  •  一整个雨季
    2020-12-30 00:13

    since your date strings seem to be in a proper format you could just do:

    >>> sorted(mydict.items())         # iteritems in py2k
    [('2000-01-01', {'fld_2': 42, 'fld_1': 1}), ('2000-01-02', {'fld_2': 22.17, 'fld_1': 23})]
    

提交回复
热议问题