简体   繁体   English

Ruby-按键对哈希数组进行分组

[英]Ruby - Group an array of hashes by key

I have an array of hashes ( edited ): 我有一个哈希数组(已编辑 ):

data = [
    {id: 1, name: "Amy", win: 1, defeat: 0},
    {id: 1, name: "Amy", win: 1, defeat: 3},
    {id: 2, name: "Carl", win: 0, defeat: 1},
    {id: 2, name: "Carl", win: 2, defeat: 1}
]

How can I group or merge into something like this using the key "name" as reference: 如何使用键“名称”作为参考来分组或合并成类似的内容:

data = [
    {id: 1, name: "Amy", win: 2, defeat: 3},
    {id: 2, name: "Carl", win: 2, defeat: 2}
]

edited I forgot to mention that a I have ID too that can't be added. 编辑后,我忘了提及我也无法添加的ID。

Here is my try 这是我的尝试

#!/usr/bin/env ruby

data = [
    {"name"=> "Amy", "win" => 1, "defeat" => 0},
    {"name"=> "Amy", "win" => 1, "defeat" => 3},
    {"name"=> "Carl", "win" => 0, "defeat" => 1},
    {"name"=> "Carl", "win" => 2, "defeat" => 1}
]

merged_hash = data.group_by { |h| h['name'] }.map do |_,val| 
  val.inject do |h1,h2| 
    h1.merge(h2) do |k,o,n|
      k == 'name' ? o : o + n 
    end
  end
end

merged_hash
# => [{"name"=>"Amy", "win"=>2, "defeat"=>3},
#     {"name"=>"Carl", "win"=>2, "defeat"=>2}]

Answer to the edited post :- 回答编辑过的帖子:-

#!/usr/bin/env ruby

data = [
    {id: 1, name: "Amy", win: 1, defeat: 0},
    {id: 1, name: "Amy", win: 1, defeat: 3},
    {id: 2, name: "Carl", win: 0, defeat: 1},
    {id: 2, name: "Carl", win: 2, defeat: 1}
]

merged_hash = data.group_by { |h| h.values_at(:name, :id) }.map do |_,val| 
  val.inject do |h1,h2| 
    h1.merge(h2) do |k,o,n|
      %i(id name).include?(k) ? o : o + n 
    end
  end
end

merged_hash
# => [{:id=>1, :name=>"Amy", :win=>2, :defeat=>3},
#     {:id=>2, :name=>"Carl", :win=>2, :defeat=>2}]

You can do it in one pass with each_with_object and a Hash-memo with an appropriate default. 您可以使用each_with_object和具有适当默认值的Hash-memo一次完成该操作。 For example: 例如:

data.each_with_object(Hash.new { |h, k| h[k] = { :id => k.first, :name => k.last, :win => 0, :defeat => 0 } }) do |h, m|
  k = h.values_at(:id, :name)
  m[k][:win   ] += h[:win   ]
  m[k][:defeat] += h[:defeat]
end.values

The basic trick is to cache the results indexed by an appropriate key ( [ h[:id], h[:name] ] in this case) and use the values to store what you're after. 基本技巧是缓存由适当的键(在本例中为[ h[:id], h[:name] ]索引的结果),并使用值存储所要存储的内容。 The default proc on the m Hash autovivifies cached values and then you can apply simple summations during iteration. m Hash上的默认proc会自动保留缓存的值,然后您可以在迭代期间应用简单的求和。 And a final values call to unwrap the cache. 然后调用最终values以解开缓存。

Good place where you can use group_by 可以使用group_by好地方

result = []
data.group_by{|d| d[:id]}.each do {|name, records|
  win = 0
  defeat = 0

  records.each do |r|
    win += r[:win]
    defeat += r[:defeat]
  end
  f = records.first
  results << {:id => f[:id], :name => f[:name], :win => win, :defeat => defeat}
end

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

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