我对计算非常陌生,并且我们被要求创建一个索引,该索引一次读取一行文本,记下特定单词以及它们出现在哪些行上。但是,我设法做到这一点,如果一个单词不止一次出现在同一行上,它会打印两次,这不适用于我的测试。删除字典中的重复条目
line = 1
x = raw_input ("Type in line 1 of the paragraph: ").lower()
text = []
d = {}
while x != ".":
x = convert_sentence(x)
text = [x]
text = string.join(text)
text = string.split(text)
for word in text:
if word in d:
d[ word ] += [line]
else:
d[ word ] = [line]
x = raw_input ("Enter a full stop to stop: ").lower()
line += 1
print "the index is"
for index in d:
print index, ":", d[ index ]
这是当我运行它产生的输出:
the index is:
blow : [1, 1]
north : [2, 2]
brisk : [1]
youth : [2]
yesteryear : [4]
wind : [1, 3, 4]
能否请你帮我找出我做错了吗?
附加继续d [文字]后+ =行 –
只是检查'在d [文字]如果不是线',如果是添加它。简单。 –