我正在尝试使用一堆数字单词对并根据常用数字对单词进行分组。我可以匹配数字,合并共享数字的子数组,并擦除这些子数组中的第一个。但是当我尝试删除第二个时,我收到了这个错误:
“在block in <main>': undefined method
[]' 中为 nil:NilClass (NoMethodError)”
有罪的行 -- ary.delete_at(i+1) -- 已被注释掉。次要问题:MRBTree 没有将嵌套数组作为输入......
ary = [[2.28, "cat"], [2.28, "bat"], [2.327, "bear"], [2.68, "ant"], [2.68, "anu"]]
i = 0
for i in 0 ... ary.size - 1
if ary[i][0] == ary[i+1][0]
b = (ary[i]+ary[i+1]).uniq
ary.delete_at(i)
# ary.delete_at(i+1)
c = [b.first], b.pop(b.length - 1)
h = Hash[*c]
ary.push(*h)
# mrbtree = MultiRBTree[c]
end
end
puts ary.inspect
输出:
# => [
# => [2.28, "bat"],
# => [2.327, "bear"],
# => [2.68, "anu"],
# => [
# => [2.28], ["cat", "bat"]
# => ],
# => [
# => [2.68], ["ant", "anu"]
# => ]
# => ]
任何帮助表示赞赏!