我对计算非常陌生,我们被要求制作一个索引,一次读取一行文字,写下特定的单词,以及它们出现在哪一行。我已设法做到这一点,但是,如果一个单词出现在同一行不止一次,它会打印两次的行,这是我的测试不允许的。
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]
请你帮我弄清楚我做错了什么?
答案 0 :(得分:0)
您只需检查找到的行是否尚未添加到条目中。为此,您需要检查if not line in d[word]
。另外,要向list
添加元素,您可以使用.append()
方法,这比+
运算符更易于理解。
这是正确的代码:
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:
if not line in d[word]:
d[word].append(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]