// An highlighted block'''
9.4 Write a program to read through the mbox-short.txt and
figure out who has 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.'''
name =input("Enter file:")iflen(name)<1: name ="mbox-short.txt"
handle =open(name)
count =dict() #初始化一个字符串
greatkey = None #不赋予其任何类型
greatvalues = None
#将文件按行打开,并找到其中以From开头的行,取出此行中的第二个字符串并对其进行计数操作
for line in handle:
words = line.split()iflen(words)<3 or words[0]!='From': #并找到其中以From开头的行
continue
count[words[1]]= count.get(words[1],0)+1 #计数
#遍历字典中的每一个键和值,找出值最大的键值对,
for k,v in count.items():if greatvalues is None or v>greatvalues: #找出值最大的键值对
greatkey = k
greatvalues = v
print(greatkey,greatvalues)