-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercise4.py
31 lines (25 loc) · 843 Bytes
/
Exercise4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
Exercise 4
Add code to the above program to figure out who has the most messages in the
file.
After all the data has been read and the dictionary has been created, look
through the dictionary using a maximum loop (see Section ??) to find who has
the most messages and print how many messages the person has.
Enter a file name: mbox-short.txt
cwen@iupui.edu 5
Enter a file name: mbox.txt
zqian@umich.edu 195
"""
file = open("mbox-short.txt")
lines = [line.strip("\n") for line in file if line.startswith("From ")]
dictionary = {}
for line in lines:
words = line.split()
dictionary[words[1]] = dictionary.get(words[1], 0) + 1
maximum = 0
winner_address = None
for address in dictionary:
if dictionary[address] > maximum:
maximum = dictionary[address]
winner_address = address
print(winner_address, maximum)