-
Notifications
You must be signed in to change notification settings - Fork 153
/
exercise10_1.py
executable file
·51 lines (39 loc) · 1.51 KB
/
exercise10_1.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
47
48
49
50
51
#!/usr/bin/env python3
"""
Exercise 10.1: Revise a previous program as follows: Read and parse the "From"
lines and pull out the addresses from the line. Count the number of messages
from each person using a dictionary.
After all the data has been read, print the person with the most commits by
creating a list of (count, email) tuples from the dictionary. Then sort the
list in the reverse order and print out the person who has the most commits.
Sample line:
From stephen.marquard@uct.ac.az Sat Jan 05 09:14:16 2008
Enter a file name: mbox-short.txt
cwen@iupui.edu 5
Enter a file name: mbox.txt
zqian@umich.edu 195
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
"""
dictionary_addresses = dict() # Initialize variables
lst = list()
fname = input('Enter file name: ')
try:
fhand = open(fname)
except FileNotFoundError:
print('File cannot be opened:', fname)
quit()
for line in fhand:
words = line.split()
if 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
for key, val in list(dictionary_addresses.items()):
lst.append((val, key)) # Fills list with value, key of dict
lst.sort(reverse=True) # Sorts by highest value
for count, email in lst[:1]: # Only displays the largest value
print(email, count)