我遇到了一个问题,而不是追加[“02.html”,7]的数值列表里面 ,它创造了“字”, 放[“02一个全新的项目。 html“,7]在散列末尾。
我没有看到的是:
invindex = {
word1: [
['01.html', 2],
]
}
tokens = %i[
word1
word2
word3
]
doc_name = '02.html'
tokens.each do |token|
if !invindex.include?(token)
invindex[token] = [[doc_name, 1]] #adds the word to the hash with the doc name and occurrence of 1
else
invindex[token].each do |list|
if list[0] == doc_name
list[1] += 1 #adds one to the occurrence with the same doc_name
else
invindex[token].insert([doc_name, 1]) #this SHOULD append the doc name and initial occurrence inside the word's value list since the word is already in the hash
end
end
end
end
p invindex
--output:--
{:word1=>[["01.html", 2]], :word2=>[["02.html", 1]], :word3=>[["02.html", 1]]}
invindex[token].insert([doc_name, 1]) #this SHOULD append the doc name
都能跟得上:
invindex = {
word: [
['01.html', 2],
]
}
token = :word
doc_name = '02.html'
invindex[token].insert([doc_name, 7])
p invindex
invindex[token].insert(-1, ["02.html", 7])
p invindex
--output:--
{:word=>[["01.html", 2]]}
{:word=>[["01.html", 2], ["02.html", 7]]}
Array#insert()需要指定一个索引作为第一个参数。一般来说,当你想添加一些东西到最后,你用<<
:
invindex = {
word: [
['01.html', 2],
]
}
token = :word
doc_name = '02.html'
invindex[token] << [doc_name, 7]
p invindex
--output:--
{:word=>[["01.html", 2], ["02.html", 7]]}
for token in tokens
Ruby开发者不使用for-in
循环,因为,在循环中调用each()
,所以Ruby开发者调用each()
直接:
tokens.each do |token|
...
end
最后,indenting in ruby
是2个空格 - 不是3个空格,而不是1个空格,而不是4个空格。它是2个空格。
运用一切都交给你的代码:
invindex = {
word1: [
['01.html', 2],
]
}
tokens = %i[
word1
word2
word3
]
doc_name = '01.html'
tokens.each do |token|
if !invindex.include?(token)
invindex[token] = [[doc_name, 1]] #adds the word to the hash with the doc name and occurrence of 1
else
invindex[token].each do |list|
if list[0] == doc_name
list[1] += 1 #adds one to the occurrence with the same doc_name
else
invindex[token] << [doc_name, 1] #this SHOULD append the doc name and initial occurrence inside the word's value list since the word is already in the hash
end
end
end
end
p invindex
--output:--
{:word1=>[["01.html", 3]], :word2=>[["01.html", 1]], :word3=>[["01.html", 1]]}
但是,仍然是一个问题,这是由于这样的事实,你改变了这一切,你都在加紧通过阵列 - 一大禁忌在计算机编程:
invindex[token].each do |list|
if list[0] == doc_name
list[1] += 1 #adds one to the occurrence with the same doc_name
else
invindex[token] << [doc_name, 1] #***PROBLEM***
看看会发生什么:
invindex = {
word1: [
['01.html', 2],
]
}
tokens = %i[
word1
word2
word3
]
%w[ 01.html 02.html].each do |doc_name|
tokens.each do |token|
if !invindex.include?(token)
invindex[token] = [[doc_name, 1]] #adds the word to the hash with the doc name and occurrence of 1
else
invindex[token].each do |list|
if list[0] == doc_name
list[1] += 1 #adds one to the occurrence with the same doc_name
else
invindex[token] << [doc_name, 1] #this SHOULD append the doc name and initial occurrence inside the word's value list since the word is already in the hash
end
end
end
end
end
p invindex
--output:--
{:word1=>[["01.html", 3], ["02.html", 2]], :word2=>[["01.html", 1], ["02.html", 2]], :word3=>[["01.html", 1], ["02.html", 2]]}
问题1:每次检查的子阵列都不包含doc_name
时,您不希望插入[doc_name, 1]
- 在所有子阵列检查完成后您只想插入[doc_name, 1]
,并且doc_name
不是找到。如果使用开始的哈希运行上面的示例:
invindex = {
word1: [
['01.html', 2],
['02.html', 7],
]
}
...您会看到输出更糟。
问题2:追加[doc_name, 1]
的阵列,而你是通过数组步进意味着[doc-name, 1]
将进行检查,也当循环获取到数组的结尾 - 然后你的循环将增加其计数到2.规则是:不要更改你正在通过的数组,因为不好的事情会发生。
谢谢你的帮助。我接受了您的建议,并在我重复完成时避免编辑阵列。我最终创建了一个“包含”变量,如果其中一个子数组具有doc_name,它将从False更改为True。在迭代结束时,如果contains仍然是False,那么我会在最后添加新的子列表。 我是Ruby的新手,一般编程,我一直在抛弃深刻的一面,清楚我有很多东西要学,所以谢谢! – jblittle