I have a list of tuples, say (name, number, birthday, gender). If I wanted to reverse sort this list by their birthday, how could I sort this in python?
This returns a new object:
>>> import operator
>>> sorted(my_list, key=operator.itemgetter(2), reverse=True)
Or, in-place:
>>> import operator
>>> mylist.sort(key=operator.itemgetter(2), reverse=True)
If you want to sort by two values; assuming tuples are like (name, birthday, time);
>>> mylist.sort(key=operator.itemgetter(1, 2), 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.