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

huangapple go评论110阅读模式
英文:

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)

尝试了上述代码并得到以下输出:
{'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}
5

最后的输出应该是:
最多的电子邮件由:'cwen@iupui.edu': 5 发送
最少的电子邮件由:'antranig@caret.cam.ac.uk': 1 发送

英文:

The task is to also print the email address that sent max emails along with the email count and email address that sent min emails with email count.

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)

Tried above code and got following output:
{'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}
5

The last output should be like
Maximum emails were sent by: 'cwen@iupui.edu': 5
Minimum emails were sent by: 'antranig@caret.cam.ac.uk': 1

答案1

得分: 0

如果你在daywise_mail_count中有你的字典(如你在问题中所述),那么你可以这样做:

# 你问题中的字典:
daywise_mail_count = {
    "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,
}

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"最多的邮件由:{mx_email} 发送,数量为:{mx_count}")
print(f"最少的邮件由:{mn_email} 发送,数量为:{mn_count}")

输出结果为:

最多的邮件由:cwen@iupui.edu 发送,数量为:5
最少的邮件由:wagnermr@iupui.edu 发送,数量为:1
英文:

If you have your dictionary in daywise_mail_count (as you stated in your question) then you can do:

# your dictionary from the question:
daywise_mail_count = {
    "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,
}

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}")

Prints:

Maximum emails were sent by: cwen@iupui.edu 5
Minimum emails were sent by: wagnermr@iupui.edu 1

huangapple
  • 本文由 发表于 2023年8月8日 22:48:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定