开发者

How to sort this list in Python?

开发者 https://www.devze.com 2022-12-08 08:43 出处:网络
[ {\'time\':33}, {\'time\':11}, {\'time\':66} ] How to sort 开发者_开发问答by the \"time\" element, DESC.Like this:

[ {'time':33}, {'time':11}, {'time':66} ]

How to sort 开发者_开发问答by the "time" element, DESC.


Like this:

from operator import itemgetter
l = sorted(l, key=itemgetter('time'), reverse=True)

Or:

l = sorted(l, key=lambda a: a['time'], reverse=True)

output:

[{'time': 66}, {'time': 33}, {'time': 11}]

If you don't want to keep the original order you can use your_list.sort which modifies the original list instead of creating a copy like sorted(your_list)

l.sort(key=lambda a: a['time'], reverse=True)
0

精彩评论

暂无评论...
验证码 换一张
取 消