#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.
name = input("Enter file:")
count = []
handle = open(name)
for line in handle:
if not line.startswith('From:'):
continue
word = line.split()
names = word[1]
full_name = names.split('@')
finally_name = full_name[0]
print(finally_name)
for count_name in finally_name: 避免遍历了单个字母,不要乱循环了
print(count_name)
---------纠正---------
name = input("Enter file:")
count = []
handle = open(name)
for line in handle:
if not line.startswith('From:'):
continue
word = line.split()
names = word[1]
full_name = names.split('@')
finally_name = full_name[0]
count.append(finally_name) 这里是把重复的也加入了这个字典,但是如果不想要重复的可以使用 if not in
print(count)
-----------找发送邮件最多的人-------
name = input("Enter file:")
count = []
counts = dict() //这里要记得是字典,因为无法在list使用get()
handle = open(name)
for line in handle:
if not line.startswith('From:'):
continue
word = line.split()
names = word[1]
count.append(names)
for key in count:
counts[key] = counts.get(key,0)+1
big_k = None //这里开始找出现次数最多的item
big_v = None
for k,v in counts.items():
if big_v is None or v > big_v:
big_v = v
big_k = k
print(big_k,big_v)