Pythonic way to iterate over a collections.Counter() instance in descending order?

后端 未结 3 1624

In Python 2.7, I want to iterate over a collections.Counter instance in descending count order.

>>> import collections
>>> c = co         


        
3条回答
  •  逝去的感伤
    2021-02-02 06:22

    Here is the example to iterate the Counter in Python collections:

    >>>def counterIterator(): 
     import collections
     counter = collections.Counter()
     counter.update(('u1','u1'))
     counter.update(('u2','u2'))
     counter.update(('u2','u1'))
     for ele in counter:
      print(ele,counter[ele])
    >>>counterIterator()
    u1 3
    u2 3
    

提交回复
热议问题