除了做列表理解逆序列表理解,有没有pythonic的方式来按值排序计数器?如果是这样,这是比这更快:如何按价值对计数器进行排序? - python
>>> from collections import Counter
>>> x = Counter({'a':5, 'b':3, 'c':7})
>>> sorted(x)
['a', 'b', 'c']
>>> sorted(x.items())
[('a', 5), ('b', 3), ('c', 7)]
>>> [(l,k) for k,l in sorted([(j,i) for i,j in x.items()])]
[('b', 3), ('a', 5), ('c', 7)]
>>> [(l,k) for k,l in sorted([(j,i) for i,j in x.items()], reverse=True)]
[('c', 7), ('a', 5), ('b', 3)
另一种方式来排序逆转是关键功能设置为'LAMDA我:-i [1 ]' –