-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMostCommonEmail.py
57 lines (40 loc) · 1.13 KB
/
MostCommonEmail.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
52
53
54
55
56
57
# Seeking email that sent the most emails from email logs (text file)
# 1. Request file name
fname = raw_input('Enter file name: ')
# 2. File handle
fhandle = open(fname)
# 3. Create dictionary
counts = dict()
# 4. Variable: first word to look for in each sentence
x = 'From'
# 5. Loop through each sentence
for line in fhandle:
# 6. Create list
words = line.split()
# 7. Guardian pattern for empty lines
if len(words) < 1:
continue
# To ignore all sentences starting from "From:"
# if words[0] == 'From:':
# continue
# 8. Ignore all other than x
if words[0] != x:
continue
# print words
# shows position 2 has the emails
email = words[1]
# 9. Map key-value pairs to dictionary
counts[email] = counts.get(email, 0) + 1
# print counts
# shows you have a dictionary with key-value pairs of format email:count
# 10. Convert dictionary to list of tuples
counts = counts.items()
# 11. Loop through lists to convert to value-key tuples
lst = list()
for k, v in counts:
lst.append((v, k))
lst = sorted(lst)
# Least common
print lst[0]
# Most common
print lst[-1]