如下所示:
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排序方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。