2014年7月28日 星期一

Dictionary using

name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()
lines = ''
words = list()
for line in handle:
    line = line.rstrip()
    if line.startswith('From:'):
        line = line.split()
        lines += line[1] + ' ' # Add only all emails to lines, get rid of From:
words = lines.split()
for word in words:
    counts[word] = counts.get(word,0) + 1  # Create dict and count at the same time.
print counts  
bigcount = None
bigword = None
for word,count in counts.items():
    if bigcount == None or count > bigcount:
        bigword = word
        bigcount = count
print bigword + ' ' + str(bigcount)
     

Output:
cwen@iupui.edu 5

沒有留言:

張貼留言