我有一个元组列表:
li = [('fink', 3), ('flip', 3), ('flock', 4), ('foo', 12), ('foot', 20), ('football', 20), ('futz', 10), ('flip', 3), ('flank', 3), ('flop', 3)]
如何先按下降号对列表进行排序,然后按字母顺序降序?结果将是:
('foot', 20)
('football', 20)
('foo', 12)
('futz', 10)
('flock', 4)
('fink', 3)
('flip', 3)
('flake', 3)
('flop', 3)
from operator import itemgetter
sorted_li = sorted(li, key=itemgetter(1,0), reverse=True)
虽然上面的代码正确地按数字降序排序,但单词没有按字母顺序排序。