1

我正在尝试使用一堆数字单词对并根据常用数字对单词进行分组。我可以匹配数字,合并共享数字的子数组,并擦除这些子数组中的第一个。但是当我尝试删除第二个时,我收到了这个错误:

“在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"]
# =>   ]
# => ]

任何帮助表示赞赏!

4

3 回答 3

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 }
于 2013-09-03T15:36:26.980 回答
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"]}
于 2013-09-03T15:46:58.870 回答
0

之后的样子

ary.delete_at(i)

数组的大小减一,因此i优于i+1

# ary.delete_at(i+1)
ary.delete_at(i)
于 2013-09-03T15:32:08.393 回答