Sorting a dictionary (with date keys) in Python

后端 未结 6 926
轮回少年
轮回少年 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:04

    I'm sure that python knows how to compare dates. So:

    def sortedDictValues(adict):
     items = adict.items()
     items.sort()
     return [value for key, value in items]
    

提交回复
热议问题