day1/exercise/kwfreq.py
changeset 180 e442b9e23972
parent 86 f657495cf8b2
child 380 669b72283b55
equal deleted inserted replaced
178:8a3a9d98fa84 180:e442b9e23972
     1 import keyword
     1 import keyword
     2 f = open('/path/to/file')
     2 f = open('amicable.py')
     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[key]
    10             if key in freq:
    11             freq[key] = value + 1
    11                 freq[key] += 1
       
    12             else:
       
    13                 freq[key] = 1
    12 
    14 
    13 print freq
    15 print freq
    14 
    16