Created
October 28, 2011 04:43
-
-
Save xiaom/1321643 to your computer and use it in GitHub Desktop.
counting frequency of word
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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