美文网首页
【Python notes】Sort Python dict b

【Python notes】Sort Python dict b

作者: 爱睡的蟹老板 | 来源:发表于2019-04-18 21:07 被阅读0次

I have a Python dictionary like this:

{
   'apple': datetime.datetime(2012, 12, 20, 0, 0, tzinfo=<UTC>),
   'orange': datetime.datetime(2012, 2, 4, 0, 0, tzinfo=<UTC>),
   'raspberry': datetime.datetime(2013, 1, 9, 0, 0, tzinfo=<UTC>)
}

Solution:

sorted(dct.items(), key=lambda p: p[1], reverse=True)

Output:

=> [('raspberry', datetime.datetime(2013, 1, 9, 0, 0)),
    ('apple', datetime.datetime(2012, 12, 20, 0, 0)),
    ('orange', datetime.datetime(2012, 2, 4, 0, 0))]

ref: answer on stackoverflow

相关文章

网友评论

      本文标题:【Python notes】Sort Python dict b

      本文链接:https://www.haomeiwen.com/subject/dcsxgqtx.html