Sort a list of tuples by value and then alphabetic

2019-02-22 10:16发布

Bit of a python newbie, but I got the following list of tuples. I need to sort it by value and if the value is the same, solve ties alphabetically. Here's a sample:

#original
list_of_medals = [('Sweden', 24), ('Germany', 16), ('Russia', 10), ('Ireland', 10), ('Spain', 9), ('Albania', 8), ('Lithuania', 7), ('Iceland', 6), ('Malta', 5), ('Italy', 5), ('Serbia', 4), ('Estonia', 4), ('Turkey', 4), ('Moldova', 2), ('Azerbaijan', 2)]
#                                                              \____/                                                                                                                      \_____/                                         \______/
#after sorting                                                 /    \                                                                                                                      /     \                                         /      \
sorted_medals  = [('Sweden', 24), ('Germany', 16), ('Ireland', 10), ('Russia', 10), ('Spain', 9), ('Albania', 8), ('Lithuania', 7), ('Iceland', 6), ('Malta', 5), ('Italy', 5), ('Estonia', 4), ('Serbia', 4), ('Turkey', 4), ('Azerbaijan', 2), ('Moldova', 2)]

Is it perhaps possible with the operator module?

2条回答
We Are One
2楼-- · 2019-02-22 10:56

In this instance, I'd use a lambda function as the key argument to sort()/sorted():

In [59]: sorted(list_of_medals, key=lambda x:(-x[1],x[0]))
Out[59]: 
[('Sweden', 24),
 ('Germany', 16),
 ('Ireland', 10),
 ('Russia', 10),
 ('Spain', 9),
 ('Albania', 8),
 ('Lithuania', 7),
 ('Iceland', 6),
 ('Italy', 5),
 ('Malta', 5),
 ('Estonia', 4),
 ('Serbia', 4),
 ('Turkey', 4),
 ('Azerbaijan', 2),
 ('Moldova', 2)]

The negation of x[1] is needed to sort the medals in descending order while sorting country names in ascending order (simply setting reverse=True wouldn't achieve that).

As several people have pointed out in the comments, a more general way to do a complex sort on a compound key is to perform several sorting steps. To do this, sort on one component at a time, starting with the least significant one:

In [67]: temp = sorted(list_of_medals, key=itemgetter(0))

In [68]: sorted(temp, key=itemgetter(1), reverse=True)
Out[68]: 
[('Sweden', 24),
 ('Germany', 16),
 ('Ireland', 10),
 ('Russia', 10),
 ...

This relies on the fact that Python's sort is stable, meaning that items that compare equal are never reordered.

查看更多
你好瞎i
3楼-- · 2019-02-22 11:05

You can use the sorted function:

sorted_by_medals = sorted(list_of_medals, key=lambda tup: (-tup[1], tup[0]))
查看更多
登录 后发表回答