【Python for Everybody(Python Data Structures)】Week 5 | Chapter 9 题目汇总

PY4E课程官网:https://www.py4e.com/

参考文章(Github):

  1. ed-lau/python-for-everybody
  2. kalpesh14m/Python-For-Everybody-Answers











Assignment 9.4

Write a program to read through the mbox-short.txt and figure out who has the sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.  

编写一个程序来读取mbox-short.txt,并将其作为一个程序来使用。计算出谁发送的邮件数量最多。该程序寻找'From'行,并取其中的第二个字行作为发送邮件的人。该程序创建了一个Python字典将发件人的邮件地址映射到他们出现在的次数统计中的文件。词典制作完成后。程序使用最大循环来读取字典,找出最多产的提交者。

import sys
fname = input('Enter the file name:  ')
try:
    fhand = open(fname)
except:
    print(fname, 'File does not exist ')
    sys.exit()

sender_dict = dict()

for line in fhand:
    words = line.split()
    if words and words[0] == 'From':
        sender_dict[words[1]] = sender_dict.get(words[1], 0)+1

bigcount = None
bigname = None
for name, count in sender_dict.items():
    if bigcount is None or count > bigcount:
        bigname = name
        bigcount = count

print(sender_dict)
print("The most prolific committer is", bigname,"and it has permitted",bigcount,"times.")

>>>Enter the file name: mbox-short.txt
{'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}
The most prolific committer is cwen@iupui.edu and it has permitted 5 times.

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值