I have a list like this one:
list1 = [['art_25', 'title', 'author'], ['art_12', 'title', 'author'], ['art_4', 'title', 'author']]
How can I sort this list st the output equals
[['art_4', 'title', 'author'], ['art_12' ...], ['art_25', ...]] ?
Thanks for any help!
You can use sorted
with the key
parameter. You can make a lambda
expression that splits on the '_'
character, turns it into an int
then sorts by that value.
list1 = [['art_25', 'title', 'author'],
['art_12', 'title', 'author'],
['art_4', 'title', 'author']]
>>> sorted(list1, key=lambda i: int(i[0].split('_')[1]))
[['art_4', 'title', 'author'],
['art_12', 'title', 'author'],
['art_25', 'title', 'author']]
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.