如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
keyword">import time def date_compare(item1, item2): t1 = time.mktime(time.strptime(item1, '%Y/%m/%d' )) t2 = time.mktime(time.strptime(item2, '%Y/%m/%d' )) print (t1, t2) if t1 < t2: return - 1 elif t1 > t2: return 1 else : return 0 if __name__ = = '__main__' : d = { '2016/08/17' : 'qqq' , '2016/08/19' : 'qqq' , '2016/08/16' : 'qqq' , '2016/11/15' : 'qqq' , '2016/08/12' : 'qqq' , '2016/08/19' : 'qqq' } keys = d.keys() q = sorted (keys, date_compare) print (keys) |
以上这篇python 对key为时间的dict排序方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/w616358337/article/details/62418678