Topic 34: Ex-6: Count Number of Messages From Each Email Address

My code:

def count_message_from_email():
file=open(’/cxldata/datasets/project/mbox-short.txt’)
email_ids=list()
email_ids_count=dict()
for line in file:
line.rstrip()
if line.startswith(‘From:’):
x=line.split()
for email in x:
email_ids.append(x[1])
for ids in email_ids:
email_ids_count[ids]=email_ids_count.get(ids,0)+1
return email_ids_count

count_message_from_email()

OUTPUT:

{‘stephen.marquard@uct.ac.za’: 4,
‘louis@media.berkeley.edu’: 6,
‘zqian@umich.edu’: 8,
‘rjlowe@iupui.edu’: 4,
‘cwen@iupui.edu’: 10,
‘gsilver@umich.edu’: 6,
‘wagnermr@iupui.edu’: 2,
‘antranig@caret.cam.ac.uk’: 2,
‘gopal.ramasammycook@gmail.com’: 2,
‘david.horwitz@uct.ac.za’: 8,
‘ray@media.berkeley.edu’: 2}

All counted values are twice than the expected output mentioned in the problem, why?
Saw the code in the answer, but the logic seems same to me. Kindly advise if I am missing something.

Thanks.

Hi, Abhinav.

You are doing a redundant steps in the for loop.
I have corrected it for you.

Note :- Try to always print each steps for easy understanding!

All the best!

Yes I was appending a single email id two times! Thanks for the reply!