Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 合并嵌套数组并删除已合并的子数组?_Ruby_Arrays_T9 - Fatal编程技术网

Ruby 合并嵌套数组并删除已合并的子数组?

Ruby 合并嵌套数组并删除已合并的子数组?,ruby,arrays,t9,Ruby,Arrays,T9,我试着用一堆数字-单词对,根据常用数字对单词进行分组。我可以匹配这些数字,合并共享该数字的子阵列,并删除第一个子阵列。但是,当我尝试删除第二个时,会出现以下错误: “在块中”:nil的未定义方法[]:NilClass(NoMethodError)” 有罪的一行(i+1处的ari.delete_)已被注释掉。第二个问题:MRBTree没有将嵌套数组作为输入 ary = [[2.28, "cat"], [2.28, "bat"], [2.327, "bear"], [2.68, "ant"], [2

我试着用一堆数字-单词对,根据常用数字对单词进行分组。我可以匹配这些数字,合并共享该数字的子阵列,并删除第一个子阵列。但是,当我尝试删除第二个时,会出现以下错误:

“在<代码>块中”:nil的未定义方法[]:NilClass(NoMethodError)”

有罪的一行(i+1处的ari.delete_)已被注释掉。第二个问题: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"]
# =>   ]
# => ]
感谢您的帮助

看起来像是

ary.delete_at(i)
数组的大小减少了1,因此
i
优于
i+1

# ary.delete_at(i+1)
ary.delete_at(i)

您的尝试失败,因为您正在修改循环中的数组(这会影响
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 }
h=Hash.new{| a,k | a[k]=[]}}一个默认值为[]的哈希值

p ary.inject(h){| a,(k,v)| a[k]转换为散列的替代版本:

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"]}
ari=[[2.28,“猫”],[2.28,“蝙蝠”],[2.327,“熊”],[2.68,“蚂蚁”],[2.68,“无尾熊”]]
hsh={}
ary.each{124; pair{124; hsh[pair[0]]].nil??hsh[pair[0]=[pair[1]:hsh[pair[0]]]{2.28=>[“猫”、“蝙蝠”]、2.327=>[“熊”]、2.68=>[“蚂蚁”、“阿努”]}

我认为最好公开您想要的结果。您想要的最终输出是什么?