Churn Emails – Count Number of Messages
Python Project – Churn Emails – Count Number of Messages From Each Email Address
Write a function count_message_from_email
which reads the file /datasets/project/mbox-short.txt
.
This function builds a histogram using a dictionary to count how many messages have come from each email address and returns the dictionary.
Output
If your logic is correct then your function should return a dictionary like the following:
{'stephen.marquard@uct.ac.za': 2, 'louis@media.berkeley.edu': 3, 'zqian@umich.edu': 4, 'rjlowe@iupui.edu': 2, 'cwen@iupui.edu': 5, 'gsilver@umich.edu': 3, 'wagnermr@iupui.edu': 1, 'antranig@caret.cam.ac.uk': 1, 'gopal.ramasammycook@gmail.com': 1, 'david.horwitz@uct.ac.za': 4, 'ray@media.berkeley.edu': 1}
def count_message_from_email():
with open("/datasets/project/mbox-short.txt") as f:
emails = [i.split(' ')[1] for i in f if i.startswith('From ')]
dic = {}
for email in emails:
dic[email] = emails.count(email)
return dic
You can use “Hint” and “See Answer” if you are stuck.