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

后端 未结 3 1612

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:28

    You can iterate over c.most_common() to get the items in the desired order. See also the documentation of Counter.most_common().

    Example:

    >>> c = collections.Counter(a=1, b=999)
    >>> c.most_common()
    [('b', 999), ('a', 1)]
    

提交回复
热议问题