Tag: 柜台

如何按价值sorting计数器? – python

除了列表理解逆序列表理解,有没有pythonic的方式来按价值sorting计数器? 如果是这样,它比这个更快: >>> 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), […]