基于哈希中其他字段的哈希数组

时间:2010-12-06 17:31:01

标签: ruby arrays hash multidimensional-array

我有一系列哈希,如下所示:

[{:head_count=>150.5, :group=>"NW", :estimated_ship_date=>"201105", :yard=>"wcc"}, 
{:head_count=>201.0, :group=>"NW", :estimated_ship_date=>"201105", :yard=>"wcc"}, 
{:head_count=>103.5, :group=>"NW", :estimated_ship_date=>"201104", :yard=>"nyssa"}]

我希望从这些数据创建一个哈希,如下所示:

{ 
  "wcc" => 
  { 
    "NW" => 
    { 
      "201105" => 351.5 # This being a sum
    }
  }
  "nyssa" =>
  {
    "NW" => 
    {
      "201104" => 103.5 # This being a sum
    }
  }
}

我不确定我能提供的其他数据。

我尝试过收集,但无法确定从哪里开始。

谢谢

4 个答案:

答案 0 :(得分:3)

稍微不同的版本使用哈希的默认proc来自动生存容器:

a = [
  {:head_count=>150.5, :group=>"NW", :estimated_ship_date=>"201105", :yard=>"wcc"}, 
  {:head_count=>201.0, :group=>"NW", :estimated_ship_date=>"201105", :yard=>"wcc"}, 
  {:head_count=>103.5, :group=>"NW", :estimated_ship_date=>"201104", :yard=>"nyssa"}
]

yards = Hash.new{ |h,yard|
  h[yard]=Hash.new{ |h,group|
    h[group]=Hash.new{ |h,est_date|
      h[est_date]=0
    }
  }
}
a.each do |h|
  yards[h[:yard]][h[:group]][h[:estimated_ship_date]] += h[:head_count]
end

p yards
#=> {"wcc"=>{"NW"=>{"201105"=>351.5}}, "nyssa"=>{"NW"=>{"201104"=>103.5}}}

答案 1 :(得分:1)

我只是使用一个简单的循环。

a = [{:head_count=>150.5, :group=>"NW", :estimated_ship_date=>"201105", :yard=>"wcc"}, 
{:head_count=>201.0, :group=>"NW", :estimated_ship_date=>"201105", :yard=>"wcc"}, 
{:head_count=>103.5, :group=>"NW", :estimated_ship_date=>"201104", :yard=>"nyssa"}]

yards = {}
a.each do |h|
  yard = yards[h[:yard]] ||= {}
  group = yard[h[:group]] ||= {}
  group[h[:estimated_ship_date]] ||= 0.0
  group[h[:estimated_ship_date]] += h[:head_count]
end

# yards => {"wcc"=>{"NW"=>{"201105"=>351.5}}, "nyssa"=>{"NW"=>{"201104"=>103.5}}}

答案 2 :(得分:1)

x = [
{:head_count=>100, :group=>"NE", :estimated_ship_date=>"201103", :yard=>"wcc"},
{:head_count=>200, :group=>"NW", :estimated_ship_date=>"201104", :yard=>"wcc"},
{:head_count=>300, :group=>"NW", :estimated_ship_date=>"201105", :yard=>"wcc"},
{:head_count=>400, :group=>"NW", :estimated_ship_date=>"201105", :yard=>"wcc"},
{:head_count=>500, :group=>"NW", :estimated_ship_date=>"201104", :yard=>"nyssa"}]

p Hash[ x.group_by{ |i| i[:yard] }.map { |i,j|
    [i, Hash[ j.group_by { |i| i[:group] }.map { |i,j|
        [i, Hash[ j.group_by { |i| i[:estimated_ship_date] }.map{ |i,j|
            [i, j.map{ |i| i[:head_count] }.inject(:+)]
        } ] ]
    } ] ]
} ]

{
    "wcc"=>
    {
        "NE"=>
        {
            "201103"=>100
        },
        "NW"=>
        {
            "201104"=>200,
            "201105"=>700
        }
    },
    "nyssa"=>
    {
        "NW"=>
        {
            "201104"=>500
        }
    }
}

答案 3 :(得分:1)

这是我的解决方案。

yards = []
a.select{|x| yards << x[:yard]}
sol = Hash.new{|k,v| k[v] = Hash.new{|k,v| k[v] = {} } }
yards.uniq.map do |x|
  a.select{|y| x == y[:yard]}.flatten.map do |z|
    sol[x][z[:group]][z[:estimated_ship_date]] = 0.0
    sol[x][z[:group]][z[:estimated_ship_date]] += z[:head_count]
  end
end

你的初始数组是。解决方案是sol。我希望这看起来很可读