我正在尝试使用一堆数字对,并根据常用数字对单词进行分组。我可以匹配数字,合并共享数字的子数组,并删除第一个子数组。但是当我尝试删除第二个时,我收到了这个错误:
“在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"]
# => ]
# => ]
任何帮助表示赞赏!
答案 0 :(得分:2)
您的尝试失败,因为您正在修改循环中的数组(对a.size
有影响)。循环结束条件不会自动调整。您正在访问之前删除的内容。
如果您的阵列不是太大,则会执行以下操作:
p Hash[ary.group_by(&:first).map { | k, v | [k, v.map(&:last)] }]
# => {2.28=>["cat", "bat"], 2.327=>["bear"], 2.68=>["ant", "anu"]}
它的工作原理如下:
ary.group_by(&:first) # group the 2-elem arrays by the number, creating a hash
# like {2.28=>[[2.28, "cat"], [2.28, "bat"]], ...}
.map { | k, v | ... } # change the array of key-value pairs to
[k, v.map(&:last)] # pairs where the value-array contains just the strings
Hash[ ... ] # make the whole thing a hash again
创建一个中间数组并将其传回哈希是一些开销。如果这是一个问题,这样的事情可能会更好:
h = Hash.new { | a, k | a[k] = [] } # a hash with [] as default value
p ary.inject(h) { | a, (k, v) | a[k] << v; a }
答案 1 :(得分:0)
看起来像
之后ary.delete_at(i)
数组的大小减1,因此i
优于i+1
:
# ary.delete_at(i+1)
ary.delete_at(i)
答案 2 :(得分:0)
转换为哈希的备用版本:
ary = [[2.28, "cat"], [2.28, "bat"], [2.327, "bear"], [2.68, "ant"], [2.68, "anu"]]
hsh = {}
ary.each {|pair| hsh[pair[0]].nil? ? hsh[pair[0]] = [pair[1]] : hsh[pair[0]] << pair[1]}
puts hsh.inspect # => {2.28 => ["cat", "bat"], 2.327 => ["bear"], 2.68 => ["ant", "anu"]}