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

From Media Design: Networked & Lens-Based wiki
Jump to navigation Jump to search
#!/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