按Python日期排序的字典列表3.4 [英] Sort list of dictionaries by date in Python 3.4
本文介绍了按Python日期排序的字典列表3.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的词典列表,但有更多的词典:
I have a list of dictionaries like this but with many more dictionaries:
x = [{"duration":datetime.time(5,30),"date":datetime.date(2016,02,13)},{"duration":datetime.time(3,30),"date":datetime.date(2016,02,11)},{"duration":datetime.time(2,0),"date":datetime.date(2016,02,16)}]
有没有办法使用Python关键字(例如)按
和 date
的升序排序此列表排序 lambda
而不进行自定义排序功能?我一直在看排序
文档,并在可选的键
参数中使用 lambda
。
Is there a way to sort this list in ascending order of date
using Python keywords such as sorted
and lambda
without making a custom sorting function? I have been looking at the sorted
docs and the use of lambda
in the optional key
argument.
推荐答案
在发布问题时,我想出了我应该分享的答案。
While posting the question I figured out the answer so thought I should share.
x = sorted(x, key = lambda k: k["date"])
这篇关于按Python日期排序的字典列表3.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文