This assignment probably the toughest for me.
First, the assignment skeleton relies on week 8's assignment. This week, we are going through Dictionary material. In this assignment, it basically just 2 extra parts on top of week8
The bolded lines are the only new Dictionary stuffs.
The red line are easy. The maxval and maxkee are the tough part. Basically, relying on slides to complete it. Not completely understanding the logic.
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "XXmboxXshort.txt"
fh = open(fname)
count = 0
count_dict = dict()
for line in fh :
if line.startswith('From '):
line_words = line.split()
#print line_words[1]
count_dict[line_words[1]] = count_dict.get(line_words[1], 0) + 1
count = count + 1
#print count_dict
maxval = None
maxkee = None
for kee, val in count_dict.items():
if maxval == None: maxval = val
if maxval < val:
maxval = val
maxkee = kee
#print kee, val, maxkee, maxval
#print 'There were', count_dict
print maxkee, maxval
Second alternative without get() method.
The key for this solution is to locate the email index from the "list".
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "XXmboxXshort.txt"
fh = open(fname)
count = 0
count_dict = dict()
for line in fh :
if line.startswith('From '):
line_words = line.split()
#print line_words[1]
if line_words[1] not in count_dict:
count_dict[line_words[1]] = 1
#print count_dict
else:
count_dict[line_words[1]] = count_dict[line_words[1]] + 1
#print count_dict
maxval = None
maxkee = None
for kee, val in count_dict.items():
if maxval == None: maxval = val
if maxval < val:
maxval = val
maxkee = kee
#print kee, val, maxkee, maxval
#print 'There were', count_dict
print maxkee, maxval