Skip to content

Instantly share code, notes, and snippets.

@lambdamusic
Created February 7, 2013 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lambdamusic/4734303 to your computer and use it in GitHub Desktop.
Save lambdamusic/4734303 to your computer and use it in GitHub Desktop.
Python: Count items and sort by incidence
## {{{ http://code.activestate.com/recipes/52231/ (r1)
class Counter:
def __init__(self):
self.dict = {}
def add(self,item):
count = self.dict.setdefault(item,0)
self.dict[item] = count + 1
def counts(self,desc=None):
'''Returns list of keys, sorted by values.
Feed a 1 if you want a descending sort.'''
i = map(lambda t: list(t),self.dict.items())
map(lambda r: r.reverse(),i)
i.sort()
if desc:
i.reverse()
return i
if __name__ == '__main__':
'''Produces:
>>> Ascending count:
[[1, 'but'], [1, 'it'], [1, 'not.'], [1, 'now'], [1, 'test,'], [1, 'test.'], [1, 'was'], [2, 'Hello'], [2, 'a'], [2, 'is'], [2, 'there'], [2, 'this']]
Descending count:
[[2, 'this'], [2, 'there'], [2, 'is'], [2, 'a'], [2, 'Hello'], [1, 'was'], [1, 'test.'], [1, 'test,'], [1, 'now'], [1, 'not.'], [1, 'it'], [1, 'but']]
'''
sentence = "Hello there this is a test. Hello there this was a test, but now it is not."
words = sentence.split()
c=Counter()
for word in words:
c.add(word)
print "Ascending count:"
print c.counts()
print "Descending count:"
print c.counts(1)
## end of http://code.activestate.com/recipes/52231/ }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment