Skip to content

Instantly share code, notes, and snippets.

@xiaom
Created October 28, 2011 04:43
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 xiaom/1321643 to your computer and use it in GitHub Desktop.
Save xiaom/1321643 to your computer and use it in GitHub Desktop.
counting frequency of word
# http://stackoverflow.com/questions/893417/item-frequency-count-in-python
# defaultdict will automatically initialize values to zero.
from collections import defaultdict
words = "apple banana apple strawberry banana lemon"
d = defaultdict(int)
for word in words.split():
d[word] += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment