returning sender with most emails sent using dictionary

For students who are taking a programming course, may be restricted in what they can use, or have other unusual limitations on solving their task.

returning sender with most emails sent using dictionary

Postby adriand » Sun Sep 18, 2016 12:21 pm

For a Python course I need to write a program to read through the mbox-short.txt and figure out who has the sent the greatest number of mail messages. I've read other posts dealing with the same question, but, although I don't get a traceback error, the result is not the one desired.

The problem I need to solve:
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 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.
The sample text file is in this line: http://www.pythonlearn.com/code/mbox-short.txt

My program looks like this, but instead of returning largestvalue 5, the desired output, it returns 7, and I have no idea where that 7 comes from. The key that it returns is ok.

Code: Select all
name = raw_input("Enter file name:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()

for line in handle:
   line = line.rstrip()
   if line.startswith('From '):
      words = line.split()

for email in words:
   email = words[1]
   counts[email] = counts.get(email,0) + 1

prolemail = None
largestvalue = None
for email,count in counts.items():
   if largestvalue is None or count > largestvalue:
      prolemail = email
      largestvalue = count

print prolemail,largestvalue


cheers
Last edited by Yoriz on Sun Sep 18, 2016 12:24 pm, edited 1 time in total.
Reason: First post lock.
adriand
 
Posts: 2
Joined: Sun Sep 18, 2016 12:05 pm

Re: returning sender with most emails sent using dictionary

Postby Ofnuts » Sun Sep 18, 2016 12:52 pm

Code: Select all
name = raw_input("Enter file name:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()

### This loop iterates the file, but doesn't really create the dictionary with each entry
for line in handle:
    line = line.rstrip()
    if line.startswith('From '):
        words = line.split()
        print words

### Out of curiosity print the contents of "words"
### Actually there is no point looping on words here
### The content of the loop body should be in the loop above
for email in words:
    email = words[1]
    counts[email] = counts.get(email,0) + 1

prolemail = None
largestvalue = None
for email,count in counts.items():
    if largestvalue is None or count > largestvalue:
        prolemail = email
        largestvalue = count

print prolemail,largestvalue
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

Re: returning sender with most emails sent using dictionary

Postby adriand » Mon Sep 19, 2016 1:28 pm

Many thanks, I've re-arranged the code and ended up with the desired result.

### Out of curiosity print the contents of "words"
This returned a list of words from one line that starts with 'From '. I'm assuming it's because it takes the first line that starts with 'From ' and splits it, after which it exits the if statement.

Still curious where that value of 7 came from though...
adriand
 
Posts: 2
Joined: Sun Sep 18, 2016 12:05 pm

Re: returning sender with most emails sent using dictionary

Postby Ofnuts » Tue Sep 20, 2016 9:11 pm

There are seven words in a "From:" line.
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987


Return to Homework

Who is online

Users browsing this forum: Bing [Bot] and 1 guest