Skip to content

Instantly share code, notes, and snippets.

@zcakzwa
Created July 5, 2016 04:43
Show Gist options
  • Save zcakzwa/54b4cd2fdf81a263e2b96269cac1c283 to your computer and use it in GitHub Desktop.
Save zcakzwa/54b4cd2fdf81a263e2b96269cac1c283 to your computer and use it in GitHub Desktop.
9.4 Write a program to read through the mbox-short.txt and figure out who has the sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times th…
name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
d = dict()
for line in handle:
if not line.startswith("From "):
continue
else:
list=line.split()
list=list[1]
d[list]=d.get(list,0)+1
maxvalue = None
maxword = None
for word,count in d.items():
if maxword is None or count > maxvalue:
maxvalue=count
maxword = word
print maxword,maxvalue
@nduprincekc
Copy link

name = input("Enter file:")
if len(name) < 1:
name = "m.txt"
handle = open(name)

count = dict()

for i in handle:
i = i.rstrip()
if i.startswith('From'):
wds = i.split()
email = wds[1]
count[email] = count.get(email,0)+1

largest = 0
for i,j in count.items():
if j > largest:
largest = j
email = i

print(email,largest)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment