0

I have a Counter object that looks like this:

counts = Counter({'foo': 10, 'baz': 5, 'biff': 3, 'bonk': 1})

for k, v in counts.items(): 
    print k, v

    bonk 1
    foo 10
    baz 5
    biff 3

What is the best way to iterative over the Counter object by descending frequency of the values? Speed is important. There is an OrderCounter class in the docs, but I was looking for something simpler.

drbunsen
  • 10,139
  • 21
  • 66
  • 94
  • 3
    btw, if speed is important then [`Counter()` might not be the best](http://stackoverflow.com/a/2525617/4279) – jfs Jul 10 '12 at 18:26

1 Answers1

3

Use Counter.most_common()

>>> counts = Counter({'foo': 10, 'baz': 5, 'biff': 3, 'bonk': 1})
>>> for k, v in counts.most_common():
    print k, v
foo 10
baz 5
biff 3
bonk 1
Daenyth
  • 35,856
  • 13
  • 85
  • 124