简体   繁体   English

Ruby中的多个哈希汇总

[英]Summary of multiple hashes in Ruby

Via selection I get the following response: 通过选择,我得到以下响应:

=> [{:month=>[3, 3, 9, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
:dates=>["30.10:", "29.10:", "28.10:", "27.10:", "26.10:", "25.10:", "24.10:", "23.10:", "22.10:", "21.10:", "20.10:", "19.10:", "18.10:", "17.10:", "16.10:", "15.10:", "14.10:", "13.10:", "12.10:", "11.10:", "10.10:", "09.10:", "08.10:", "07.10:", "06.10:", "05.10:", "04.10:", "03.10:", "02.10:", "01.10:"]},
{:month=>[1, 3, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
:dates=>["30.10:", "29.10:", "28.10:", "27.10:", "26.10:", "25.10:", "24.10:", "23.10:", "22.10:", "21.10:", "20.10:", "19.10:", "18.10:", "17.10:", "16.10:", "15.10:", "14.10:", "13.10:", "12.10:", "11.10:", "10.10:", "09.10:", "08.10:", "07.10:", "06.10:", "05.10:", "04.10:", "03.10:", "02.10:", "01.10:"]}]

In this example there are two groups of hashes. 在此示例中,有两组哈希。 There could be more results but the dates will be the same every time. 可能会有更多结果,但每次的日期都相同。

Is there an easy way to combine it to the following result? 有没有简单的方法可以将其组合为以下结果?

[{:month=>[4, 6, 14, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
:dates=>["30.10:", "29.10:", "28.10:", "27.10:", "26.10:", "25.10:", "24.10:", "23.10:", "22.10:", "21.10:", "20.10:", "19.10:", "18.10:", "17.10:", "16.10:", "15.10:", "14.10:", "13.10:", "12.10:", "11.10:", "10.10:", "09.10:", "08.10:", "07.10:", "06.10:", "05.10:", "04.10:", "03.10:", "02.10:", "01.10:"]}

For Arrays I could use transpose.map {|x| x.reduce(:+)} 对于数组,我可以使用transpose.map {|x| x.reduce(:+)} transpose.map {|x| x.reduce(:+)} but not for hashes. transpose.map {|x| x.reduce(:+)}但不用于哈希。 Is there a similar solution for hashes? 是否有类似的哈希解决方案?

arr = [{ :month=>[3, 3, 9], :dates=>["30.10:", "29.10:"] },
       { :month=>[1, 3, 5], :dates=>["30.10:", "29.10:"] },
       { :month=>[2, 4, 6], :dates=>["30.10:", "29.10:"] }]

{ :month=>arr.map { |h| h[:month] }.transpose.map { |a| a.reduce(:+) },
  :dates=>arr.first[:dates] }
  #=> {:month=>[6, 10, 20], :dates=>["30.10:", "29.10:"]} 

The steps are as follows. 步骤如下。

a = arr.map { |h| h[:month] }
  #=> [[3, 3, 9], [1, 3, 5], [2, 4, 6]] 
b = a.transpose
  #=> [[3, 1, 2], [3, 3, 4], [9, 5, 6]] 
c = b.map { |a| a.reduce(:+) }
  #=> [6, 10, 20] 
d = arr.first
  #=> {:month=>[3, 3, 9], :dates=>["30.10:", "29.10:"]} 
e = d[:dates]
  #=> ["30.10:", "29.10:"] 
{ :month=>c, :dates=>e }
  #=> {:month=>[6, 10, 20], :dates=>["30.10:", "29.10:"]} 

Given the array of hashes in the response: 给定响应中的哈希值数组:

@hashes = [{:month=>[3, 3, 9, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
:dates=>["30.10:", "29.10:", "28.10:", "27.10:", "26.10:", "25.10:", "24.10:", "23.10:", "22.10:", "21.10:", "20.10:", "19.10:", "18.10:", "17.10:", "16.10:", "15.10:", "14.10:", "13.10:", "12.10:", "11.10:", "10.10:", "09.10:", "08.10:", "07.10:", "06.10:", "05.10:", "04.10:", "03.10:", "02.10:", "01.10:"]},
{:month=>[1, 3, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
:dates=>["30.10:", "29.10:", "28.10:", "27.10:", "26.10:", "25.10:", "24.10:", "23.10:", "22.10:", "21.10:", "20.10:", "19.10:", "18.10:", "17.10:", "16.10:", "15.10:", "14.10:", "13.10:", "12.10:", "11.10:", "10.10:", "09.10:", "08.10:", "07.10:", "06.10:", "05.10:", "04.10:", "03.10:", "02.10:", "01.10:"]}]

You can use inject to combine them both and get the result you supplied in your example: 您可以使用inject将两者结合起来,并获得示例中提供的结果:

result = @hashes.inject({}) do |memo, hash|
  memo[:month] ||= []
  memo[:dates] ||= []
  memo[:month] += hash[:month]
  memo[:dates] += hash[:dates]
  memo
end

Now result looks like your example: 现在结果看起来像您的示例:

result == {
  :month=>[3, 3, 9, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
  :dates=>["30.10:", "29.10:", "28.10:", "27.10:", "26.10:", "25.10:", "24.10:", "23.10:", "22.10:", "21.10:", "20.10:", "19.10:", "18.10:", "17.10:", "16.10:", "15.10:", "14.10:", "13.10:", "12.10:", "11.10:", "10.10:", "09.10:", "08.10:", "07.10:", "06.10:", "05.10:", "04.10:", "03.10:", "02.10:", "01.10:", "30.10:", "29.10:", "28.10:", "27.10:", "26.10:", "25.10:", "24.10:", "23.10:", "22.10:", "21.10:", "20.10:", "19.10:", "18.10:", "17.10:", "16.10:", "15.10:", "14.10:", "13.10:", "12.10:", "11.10:", "10.10:", "09.10:", "08.10:", "07.10:", "06.10:", "05.10:", "04.10:", "03.10:", "02.10:", "01.10:"]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM