【发布时间】:2010-05-23 12:05:45
【问题描述】:
这是我目前拥有的代码:
from collections import defaultdict
goodwords = set()
with open("soccer.txt", "rt") as f:
for word in f.readlines():
goodwords.add(word.strip())
badwords = defaultdict(list)
with open("soccer.txt", "rt") as f:
for line_no, line in enumerate(f):
for word in line.split():
if word not in text:
badwords[word].append(line_no)
print(badwords)
如何修复我的代码,以便打印存储在 words 列表中的错误单词和行号?
例如,如果单词 togeher 在第 5 行和第 7 行拼写错误,则会打印如下内容:
togeher 5 7
【问题讨论】:
-
您需要格式化代码,以便我们可以运行它 - 我做了一个简单的格式,但这缺少一些缩进
-
不计算行数;使用
len(words)。 -
如果您遇到错误并想询问相关问题,请告诉我们您遇到的什么错误。
-
我得到的错误是 d[word].append(counter) KeyError: 'a'
标签: python dictionary spell-checking