day1/exercise/kwfreq.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Mon, 26 Oct 2009 12:41:40 +0530
changeset 166 ddfd95133adc
parent 86 f657495cf8b2
child 180 e442b9e23972
permissions -rw-r--r--
Taken task based approach for Session 3, day 1. Removed introduction to dictionaries slide and accommodated the same content while solving the problem.

import keyword
f = open('/path/to/file')

freq = {}
for line in f:
    words = line.split()
    for word in words:
        key = word.strip(',.!;?()[]: ')
        if keyword.iskeyword(key):
            value = freq[key]
            freq[key] = value + 1

print freq