【发布时间】:2012-06-17 17:12:13
【问题描述】:
在 Python 2.7 中,我想以降序遍历 collections.Counter 实例。
>>> import collections
>>> c = collections.Counter()
>>> c['a'] = 1
>>> c['b'] = 999
>>> c
Counter({'b': 999, 'a': 1})
>>> for x in c:
print x
a
b
在上面的示例中,元素似乎按照它们添加到 Counter 实例的顺序进行迭代。
我想从最高到最低遍历列表。我看到 Counter 的字符串表示是这样做的,只是想知道是否有推荐的方法。
【问题讨论】:
标签: collections iteration python-2.7 python