dict_遍历错误点

#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)





转载于:https://www.cnblogs.com/fivecats/p/8652662.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值