Skip to content

Instantly share code, notes, and snippets.

@MaryamFarshbafi
Forked from MichelleDalalJian/py4e_ex_09_04
Created August 19, 2022 02:25
Show Gist options
  • Save MaryamFarshbafi/d055efbd5b3e2b41a9927e219c2f5f7e to your computer and use it in GitHub Desktop.
Save MaryamFarshbafi/d055efbd5b3e2b41a9927e219c2f5f7e 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…
fname = input("Enter file:")
if len(fname) < 1 : name = "mbox-short.txt"
hand = open(fname)
lst = list()
for line in hand:
if not line.startswith("From:"): continue
line = line.split()
lst.append(line[1])
counts = dict()
for word in lst:
counts[word] = counts.get(word,0) + 1
bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigcount = count
bigword = word
print (bigword,bigcount)
@MaryamFarshbafi
Copy link
Author

name = input("Enter file:")
if len(name) < 1:
name = "mbox-short.txt"
handle = open(name)
dic=dict()
for line in handle:
if not line.startswith("From:"): continue
line = line.split()
if line[1] in dic.keys():
dic[line[1]]+=1
else:
dic[line[1]]=1
line=[]

largest=-1

for key,value in dic.items():
if largest< value:
largest=value
key_max=key
print(key_max,largest)

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