如何总结几个数组的相应元素?

时间:2015-03-30 23:15:34

标签: ruby arrays

我有几个不同长度的数组,每个数组都有一个2项数组。例如:

[["12:00", 7.0], ["01:00", 3.3], ["02:00", 11.9], ["03:00", 56.5]]

[["12:00", 44.3], ["01:00", 2.25], ["02:00", 2.44], ["03:00", 46.11], ["04:00", 8.9], ["05:00", 18.187]]

我想循环,并将每个数组的相应元素添加到一个新数组中。输出数组将是我们加在一起的最长数组的长度。

因此,上面两个数组相加将输出以下内容:

[["12:00", 51.3], ["01:00", 5.55], ["02:00", 14.34], ["03:00", 102.61], ["04:00", 8.9], ["05:00", 18.187]]

我认为我不能使用reduce()inject(),因为我不想折叠数组,数组也不是简单的元素数组。

我真的不知道如何解决这个问题。

2 个答案:

答案 0 :(得分:4)

您可以使用Hash.merge在一行中执行此操作。使用块在合并期间对值求和。

def sum_arrays(a, b)
  Hash[a].merge(Hash[b]){|k, i, j| i + j}.to_a
end

输出:

a = [["12:00", 7.0], ["01:00", 3.3], ["02:00", 11.9], ["03:00", 56.5]]
b = [["12:00", 44.3], ["01:00", 2.25], ["02:00", 2.44], ["03:00", 46.11], ["04:00", 8.9], ["05:00", 18.187]]    
sum_arrays(a,b)

=> [["12:00", 51.3], ["01:00", 5.55], ["02:00", 14.34], ["03:00", 102.61], ["04:00", 8.9], ["05:00", 18.187]]

要对两个以上的数组求和,请再添加一行:

def sum_many_arrays(*a)
  a.reduce{|s, i| sum_arrays(s, i)}
end

输出:

sum_many_arrays([[:a,1],[:b,2]],[[:a,2],[:b,2],[:c,1]],[[:a,5],[:b,2]])
=> [[:a, 8], [:b, 6], [:c, 1]] 

答案 1 :(得分:0)

我推荐@ Oleg的方法,但总有另一种方法:

arr = [[["12:00",  7.0], ["01:00",  3.3], ["02:00", 11.9], ["03:00", 56.5]],
       [["12:00", 44.3], ["01:00", 2.25], ["02:00", 2.44], ["03:00", 46.11],
        ["04:00",  8.9]],
       [["01:00", 22.25], ["02:00", 1.84], ["12:00", 13.3]]]

keys = arr.reduce([]) { |keys,a| keys | a.map(&:first) }
arr.map { |a| a.to_h.values_at(*keys) }
   .transpose
   .map { |e| [keys.shift, e.reduce(0) { |tot,x| tot + x.to_f }] }
  #=> [["12:00",   64.6], ["01:00", 27.8], ["02:00", 16.18],
  #    ["03:00", 102.61], ["04:00",  8.9]] 

步骤:

keys = arr.reduce([]) { |keys,a| keys | a.map(&:first) }
  #=> ["12:00", "01:00", "02:00", "03:00", "04:00"] 
b = arr.map { |a| a.to_h.values_at(*keys) }
  #=> [[ 7.0,   3.3, 11.9,  56.5, nil],
  #    [44.3,  2.25, 2.44, 46.11, 8.9],
  #    [13.3, 22.25, 1.84,   nil, nil]] 
c = b.transpose
  #=> [[7.0,   44.3,  13.3],
  #    [3.3,   2.25, 22.25],
  #    [11.9,  2.44,  1.84],
  #    [56.5, 46.11,   nil],
  #    [nil,    8.9,   nil]]
c.map { |e| [keys.shift, e.reduce(0) { |tot,x| tot + x.to_f }] }
  #=> [["12:00",   64.6], ["01:00", 27.8], ["02:00", 16.18],
  #    ["03:00", 102.61], ["04:00",  8.9]] 

请注意,NilClass#to_f会将nil转换为0.0

详细说明上面b的计算:

d = arr.map
  #=> #<Enumerator: [[["12:00",   7.0], ["01:00",  3.3], ["02:00", 11.9],
  #                   ["03:00",  56.5]],
  #                  [["12:00",  44.3], ["01:00", 2.25], ["02:00", 2.44],
  #                   ["03:00", 46.11], ["04:00",  8.9]],
  #                  [["01:00", 22.25], ["02:00", 1.84], ["12:00", 13.3]]]:map> 

a = d.next
  #=> [["12:00", 7.0], ["01:00", 3.3], ["02:00", 11.9], ["03:00", 56.5]] 
e = a.to_h
  #=> {"12:00"=>7.0, "01:00"=>3.3, "02:00"=>11.9, "03:00"=>56.5} 
f = e.values_at(*keys)
  #=> e.values_at(*["12:00", "01:00", "02:00", "03:00", "04:00"] ) 
  #=> [7.0, 3.3, 11.9, 56.5, nil] 

a = d.next
  #=> [["12:00",  44.3], ["01:00", 2.25], ["02:00", 2.44],
  #    ["03:00", 46.11], ["04:00",  8.9]] 
e = a.to_h
  #=> {"12:00"=> 44.3, "01:00"=>2.25, "02:00"=>2.44,
  #    "03:00"=>46.11, "04:00"=> 8.9} 
f = e.values_at(*keys)
  #=> [44.3, 2.25, 2.44, 46.11, 8.9] 

a = d.next
  #=> [["01:00", 22.25], ["02:00", 1.84], ["12:00", 13.3]] 
e = a.to_h
  #=> {"01:00"=>22.25, "02:00"=>1.84, "12:00"=>13.3} 
f = e.values_at(*keys)
  #=> [13.3, 22.25, 1.84, nil, nil] 

c.map的计算方法如下:

keys = ["12:00", "01:00", "02:00", "03:00", "04:00"] 

d = c.map
  #=> #<Enumerator: [[ 7.0, 44.3, 13.3], [3.3, 2.25, 22.25],
  #                  [11.9, 2.44, 1.84], [56.5, 46.11, nil],
  #                  [nil, 8.9, nil]]:map> 

e = d.next
  #=> [7.0, 44.3, 13.3] 
f = keys.shift
  #=> "12:00" 
keys
  #=> ["01:00", "02:00", "03:00", "04:00"] 
e.reduce(0) { |tot,x| tot + x.to_f }
  #=> 64.6

e = d.next
  #=> [3.3, 2.25, 22.25] 
f = keys.shift
  #=> "01:00" 
keys
  #=> ["02:00", "03:00", "04:00"] 
e.reduce(0) { |tot,x| tot + x.to_f }
  #=> 27.8 

e = d.next
  #=> [11.9, 2.44, 1.84] 
f = keys.shift
  #=> "02:00" 
keys
  #=> ["03:00", "04:00"] 
e.reduce(0) { |tot,x| tot + x.to_f }
  #=> 16.18 

e = d.next
  #=> [56.5, 46.11, nil] 
f = keys.shift
  #=> "03:00" 
keys
  #=> ["04:00"] 
e.reduce(0) { |tot,x| tot + x.to_f }
  #=> 102.61 

e = d.next
  #=> [nil, 8.9, nil] 
f = keys.shift
  #=> "04:00" 
keys
  #=> [] 
e.reduce(0) { |tot,x| tot + x.to_f }
  #=> 8.9