简体   繁体   中英

How to sort this list in Python?

[ {'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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM