Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 27, 2021 03:04 pm GMT

Day 34 Of 100DaysOfCode : Python Code To Find Count Of Mail

Today is my 34 day of #100DaysOfCode and Python. Today I tried to write some code on python access to web data and data structure on python. Complete some assignment on coursera.

Below is the python code I tried to write to find the count of mail from the file and from whom maximum times mail come.

Python Code

At first we open file. Set counts to zero at first. There is empty list for mails. I write simple loop to find the mail. There is empty dictionary to store mail as a key and number of count as a value.

fhand = open('mbox-short.txt')count = 0emails = []for line in fhand:    words = line.split()    # print('Debug:', words)    if len(words) == 0 : continue    if words[0] != 'From' : continue    emails.append(words[1])#print(emails)d = {}for email in emails:    if email not in d:        d[email] = 1    else:        d[email] += 1#print(d)max(d)nd = {k: v for k, v in list(reversed(sorted(d.items(), key=lambda item: item[1])))}nd
Enter fullscreen mode Exit fullscreen mode

The output of this code is given below.

To find by whom we got maximum mail is find by

print(f"This {list(nd.items())[0][0]} send mail most i.e {list(nd.items())[0][1]} times.")
Enter fullscreen mode Exit fullscreen mode

Output is,

This cwen@iupui.edu send mail most i.e 5 times.
Enter fullscreen mode Exit fullscreen mode

Original Link: https://dev.to/iamdurga/day-34-of-100daysofcode-python-code-to-find-count-of-mail-4ddh

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To