forked from jmelahman/python-for-everybody-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise9_4.py
46 lines (36 loc) · 1.46 KB
/
exercise9_4.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
Exercise 9.4: Add ccode to the above program to figure out who has the most
mesasges 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 [maximumloop]) to
find who has the most messages and print how many messages the person has.
Enter a file name: mbox-short.txt
Enter a file name: mbox.txt
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
Solution by Jamison Lahman, May 31, 2017
"""
dictionary_addresses = dict() #Initializes the dictionary
fname = input('Enter file name: ')
try:
fhand = open(fname)
except:
print('File cannot be opened:', fname)
exit()
for line in fhand:
words = line.split()
if len(words) == 0 or len(words) < 2 or words[0] != 'From':
continue
else:
if words[1] not in dictionary_addresses:
dictionary_addresses[words[1]] = 1 #First entry
else:
dictionary_addresses[words[1]] += 1 #Additional counts
maximum = 0 #Initilizes the variable
for address in dictionary_addresses:
if dictionary_addresses[address] > maximum: #Checks if new maximum
maximum = dictionary_addresses[address] #Updates the maximum if need
maximum_address = address #Stores the address of maximum
print(maximum_address, maximum)