#Write a program to read through the mbox-short.txt
# and figure out who has 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 they appear in the file.
# After the dictionary is produced, the program reads through the dictionary
# using a maximum loop to find the most prolific committer.
filename=input("Enter the file name: ") #getting the file name
if len(filename) < 1 : filename = "mbox-short.txt"
fh=open(filename) #opening the file
email_addresses={} #making a dictionary
for line in fh:
if line.startswith("From: "): #getting the email addresses starting 'from'
email=line.split()[1] #getting only email addresses starting from position 1
email_addresses[email]=email_addresses.get(email,0) + 1 #logic for dictionary counting
#writing a maximum finding loop
largest=-1 #setting the base values and printing the largest and words associated with it
email1=None
for k,v in email_addresses.items(): #double variables for loop k = keys, v = values
if v>largest:
largest=v
theword=k
print(theword, largest)
إرسال تعليق