【发布时间】:2020-01-29 04:17:40
【问题描述】:
我想对元组列表进行排序(元组的格式为 (integer, string)),以便从最大到最小整数对它们进行排序,然后按字母顺序对字符串进行排序,而不会丢失整数顺序。
### Inputs: data = [(86, 'william'), (74, 'olivia'), (86, 'willaim'), (62, 'lyli'), (74, 'olivai'), (62, 'lily')]
### Outputs: data = [(86, 'willaim'), (86, 'william'), (74, 'olivai'), (74, 'olivia'), (62, 'lily'), (62, 'lyli')]
### What I know how to do: I know how to sort the tuples by specific element from greatest to least
import operator
data.sort(key = operator.itemgetter(0), reverse = True)
### This returns:
### data = [(86, 'william'), (86, 'willaim'), (74, 'olivia'), (74, 'olivai'), (62, 'lily'), (62, 'lyli')]
### At this point I'm not sure how to keep the integers ordered while sorting the strings alphabetically
【问题讨论】: