day1/exercise/kwfreq.py
changeset 86 f657495cf8b2
parent 64 333092b68926
child 180 e442b9e23972
equal deleted inserted replaced
85:8ca53181bee6 86:f657495cf8b2
     1 import keyword
     1 import keyword
     2 f = open('/home/madhu/pyprogs/pytriads.py')
     2 f = open('/path/to/file')
     3 
     3 
     4 freq = {}
     4 freq = {}
     5 for line in f:
     5 for line in f:
     6     words = line.split()
     6     words = line.split()
     7     for word in words:
     7     for word in words:
     8         key = word.strip(',.!;?()[]: ')
     8         key = word.strip(',.!;?()[]: ')
     9         if keyword.iskeyword(key):
     9         if keyword.iskeyword(key):
    10             value = freq.get(key, 1)
    10             value = freq[key]
    11             freq[key] = value + 1
    11             freq[key] = value + 1
    12 
    12 
    13 print freq
    13 print freq
    14 
    14