提问人:Mayur Dhandarphale 提问时间:8/8/2023 最后编辑:Mayur Dhandarphale 更新时间:8/9/2023 访问量:14
浏览文本文件。计算每个不同电子邮件地址发送的电子邮件,并打印电子邮件地址以及电子邮件计数
Go through a text file. Count the emails sent by each distinct email address and print the email address along with the count of emails
问:
该任务还打印发送最多电子邮件的电子邮件地址以及电子邮件计数和发送最少电子邮件的电子邮件地址以及电子邮件计数。
file1 = open("mbox-short.txt")
daywise_mail_count = dict()
for line in file1:
words= line.split()
if len(words) == 0 or words[0] !='From': continue
word = words[1]
if word not in daywise_mail_count:
daywise_mail_count[word]= 1
else:
daywise_mail_count[word] += 1
print (daywise_mail_count)
Keymax = max(zip(daywise_mail_count.values(), daywise_mail_count.keys()))
print (keymax)
尝试了上面的代码并得到了以下输出: {'[email protected]': 2, '[email protected]': 3, '[email protected]': 4, '[email protected]': 2, '[email protected]': 5, '[email protected]': 3, '[email protected]': 1, '[email protected]': 1, '[email protected]': 1, '[email protected]': 4, '[email protected]': 1} 5
最后一个输出应为 发送者最多的电子邮件数量为:“[email protected]”:5 发送的电子邮件最少数量为:“[email protected]”:1
答:
0赞
Andrej Kesely
8/9/2023
#1
如果你有你的字典(正如你在问题中所说的那样),那么你可以做:daywise_mail_count
# your dictionary from the question:
daywise_mail_count = {
"[email protected]": 2,
"[email protected]": 3,
"[email protected]": 4,
"[email protected]": 2,
"[email protected]": 5,
"[email protected]": 3,
"[email protected]": 1,
"[email protected]": 1,
"[email protected]": 1,
"[email protected]": 4,
"[email protected]": 1,
}
mx_email, mx_count = max(daywise_mail_count.items(), key=lambda k: k[1])
mn_email, mn_count = min(daywise_mail_count.items(), key=lambda k: k[1])
print(f"Maximum emails were sent by: {mx_email} {mx_count}")
print(f"Minimum emails were sent by: {mn_email} {mn_count}")
指纹:
Maximum emails were sent by: cwe[email protected] 5
Minimum emails were sent by: [email protected] 1
评论