繁体   English   中英

列表中项目的降序计数

[英]Count of items in a list in descending order

嗨,我有一个像这样的列表: llist=['a','b','c','b','e','a','f','e','f','e','e','e','a']我使用集合中的计数器,并使用:

from collections import Counter

c=Counter(llist)

print c.items()

它打印[('a', 3), ('c', 1), ('b', 2), ('e', 5), ('f', 2)]

我想按降序打印它们

   5 e
   3 a
   2 b
   2 f
   1 c

这有效:

>>> from collections import Counter
>>> llist = ['a','b','c','b','e','a','f','e','f','e','e','e','a']
>>> c = Counter(llist)
>>> for i,j in c.most_common():
...     print j,i
...
5 e
3 a
2 b
2 f
1 c
>>>

这是有关collections.Counter.most_common的参考。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM