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评论137阅读模式
英文:

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

问题

任务还包括打印发送最多电子邮件的电子邮件地址以及发送最少电子邮件的电子邮件地址,以及相应的电子邮件计数。

  1. file1 = open("mbox-short.txt")
  2. daywise_mail_count = dict()
  3. for line in file1:
  4. words= line.split()
  5. if len(words) == 0 or words[0] != 'From': continue
  6. word = words[1]
  7. if word not in daywise_mail_count:
  8. daywise_mail_count[word]= 1
  9. else:
  10. daywise_mail_count[word] += 1
  11. print (daywise_mail_count)
  12. Keymax = max(zip(daywise_mail_count.values(), daywise_mail_count.keys()))
  13. 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.

  1. file1 = open("mbox-short.txt")
  2. daywise_mail_count = dict()
  3. for line in file1:
  4. words= line.split()
  5. if len(words) == 0 or words[0] !='From': continue
  6. word = words[1]
  7. if word not in daywise_mail_count:
  8. daywise_mail_count[word]= 1
  9. else:
  10. daywise_mail_count[word] += 1
  11. print (daywise_mail_count)
  12. Keymax = max(zip(daywise_mail_count.values(), daywise_mail_count.keys()))
  13. 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中有你的字典(如你在问题中所述),那么你可以这样做:

  1. # 你问题中的字典:
  2. daywise_mail_count = {
  3. "stephen.marquard@uct.ac.za": 2,
  4. "louis@media.berkeley.edu": 3,
  5. "zqian@umich.edu": 4,
  6. "rjlowe@iupui.edu": 2,
  7. "cwen@iupui.edu": 5,
  8. "gsilver@umich.edu": 3,
  9. "wagnermr@iupui.edu": 1,
  10. "antranig@caret.cam.ac.uk": 1,
  11. "gopal.ramasammycook@gmail.com": 1,
  12. "david.horwitz@uct.ac.za": 4,
  13. "ray@media.berkeley.edu": 1,
  14. }
  15. mx_email, mx_count = max(daywise_mail_count.items(), key=lambda k: k[1])
  16. mn_email, mn_count = min(daywise_mail_count.items(), key=lambda k: k[1])
  17. print(f"最多的邮件由:{mx_email} 发送,数量为:{mx_count}")
  18. print(f"最少的邮件由:{mn_email} 发送,数量为:{mn_count}")

输出结果为:

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

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

  1. # your dictionary from the question:
  2. daywise_mail_count = {
  3. "stephen.marquard@uct.ac.za": 2,
  4. "louis@media.berkeley.edu": 3,
  5. "zqian@umich.edu": 4,
  6. "rjlowe@iupui.edu": 2,
  7. "cwen@iupui.edu": 5,
  8. "gsilver@umich.edu": 3,
  9. "wagnermr@iupui.edu": 1,
  10. "antranig@caret.cam.ac.uk": 1,
  11. "gopal.ramasammycook@gmail.com": 1,
  12. "david.horwitz@uct.ac.za": 4,
  13. "ray@media.berkeley.edu": 1,
  14. }
  15. mx_email, mx_count = max(daywise_mail_count.items(), key=lambda k: k[1])
  16. mn_email, mn_count = min(daywise_mail_count.items(), key=lambda k: k[1])
  17. print(f"Maximum emails were sent by: {mx_email} {mx_count}")
  18. print(f"Minimum emails were sent by: {mn_email} {mn_count}")

Prints:

  1. Maximum emails were sent by: cwen@iupui.edu 5
  2. 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:

确定