Skip to content

Commit

Permalink
add persent method to Counter (by aaronsw)
Browse files Browse the repository at this point in the history
  • Loading branch information
anandology committed Feb 18, 2010
1 parent 3d2e2b1 commit bc23efc
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions web/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@ def least(self):
"""Returns the keys with mininum count."""
m = min(self.itervalues())
return [k for k, v in self.iteritems() if v == m]

def percent(self, key):
"""Returns what percentage a certain key is of all entries.
>>> c = counter()
>>> c.add('x')
>>> c.add('x')
>>> c.add('x')
>>> c.add('y')
>>> c.percent('x')
0.75
>>> c.percent('y')
0.25
"""
return float(self[key])/sum(self.values())

def sorted_keys(self):
"""Returns keys sorted by value.
Expand Down

0 comments on commit bc23efc

Please sign in to comment.