目前正在为 Coursera 的 Python For everyone 课程做此工作。这是前 9.4。由于某种原因,emcount 是运行时应有的两倍。这个问题似乎很早就开始了,因为 line 的数量是它应该的两倍。有谁知道我哪里出错了?谢谢!
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
lst = list()
#emcount = dict()
for line in handle:
if not line.startswith("From"): continue
line=line.split()
print(line)
lst.append(line[1])#adding each email occurrence to lst
# print(lst)
emcount = dict()
for word in lst:
emcount[word] = emcount.get(word,0)+1
# print(emcount)
bigcount = 0#empty at beginning
bigword = None
for word,count in emcount.items():
#items give you acopy of each key value pair, word is the key
if bigcount> count:
bigcount = bigcount
bigword = bigword
print(bigword, bigcount)
else:
bigcount = count
bigword = word
`