Displaying a list of words from a text followed by the number of times they appear

From XPUB & Lens-Based wiki
Revision as of 14:13, 16 March 2011 by Aymeric Mansoux (talk | contribs) (Created page with "<source lang="python"> #!/usr/bin/env python import sys, string words = {} for line in sys.stdin: for word in line.split(): word = word.lower().strip(string.punctua...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
#!/usr/bin/env python

import sys, string

words = {}
for line in sys.stdin:
    for word in line.split():
        word = word.lower().strip(string.punctuation)
        words[word] = words.get(word, 0) + 1

for (word, count) in sorted(words.items()):
    print word, count