assignment 10.1

读取与解析出以“From”开头的行, 取出符合条件的行中的邮箱。 使用字典统计出每个人的邮件数。当所有的数据读取完毕, 从字典中创建一个元组(count, email)列表, 然后对列表进行反向排序, 打印出提交最多的用户

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
fhand = open('mbox-short.txt')
counts = dict()
for line in fhand:
if not line.startswith('From ') : continue
pieces = line.split()
email = pieces[1]
counts[email] = counts.get(email,0) + 1
bigcount = None
bigword = None
for email,count in counts.items():
if bigcount is None or count > bigcount:
bigcount = count
bigword = email
print bigword,bigcount