キーによるグループハッシュと値を合計します
-
10-10-2019 - |
質問
ハッシュの配列があります:
[{"Vegetable"=>10}, {"Vegetable"=>5}, {"Dry Goods"=>3>}, {"Dry Goods"=>2}]
使用する必要があります inject
ここでは、私は本当に苦労しています。
以前のハッシュの重複キーの合計を反映した新しいハッシュが必要です。
[{"Vegetable"=>15}, {"Dry Goods"=>5}]
このハッシュを出力するコードを制御しているので、必要に応じて変更できます。結果は主にハッシュでした。これは、任意の数のレベルの深さにネストされる可能性があり、アレイでフラットンを呼び出すのは簡単であるが、ハッシュのキー/値もフラット化することはないためです。
def recipe_pl(parent_percentage=nil)
ingredients.collect do |i|
recipe_total = i.recipe.recipeable.total_cost
recipe_percentage = i.ingredient_cost / recipe_total
if i.ingredientable.is_a?(Purchaseitem)
if parent_percentage.nil?
{i.ingredientable.plclass => recipe_percentage}
else
sub_percentage = recipe_percentage * parent_percentage
{i.ingredientable.plclass => sub_percentage}
end
else
i.ingredientable.recipe_pl(recipe_percentage)
end
end
end
解決
ar = [{"Vegetable"=>10}, {"Vegetable"=>5}, {"Dry Goods"=>3}, {"Dry Goods"=>2}]
p ar.inject{|memo, el| memo.merge( el ){|k, old_v, new_v| old_v + new_v}}
#=> {"Vegetable"=>15, "Dry Goods"=>5}
Hash.merge
ブロックが複製を見つけたときにブロックを実行します。 inject
初期なし memo
アレイの最初の要素をとして扱います memo
, 、ここでは問題ありません。
他のヒント
簡単に使用してください:
array = [{"Vegetable"=>10}, {"Vegetable"=>5}, {"Dry Goods"=>3}, {"Dry Goods"=>2}]
array.inject{|a,b| a.merge(b){|_,x,y| x + y}}
ar = [{"Vegetable"=>10}, {"Vegetable"=>5}, {"Dry Goods"=>3}, {"Dry Goods"=>2}]
一方 Hash.merge
テクニックは正常に機能します、私はそれがよりよく読むと思います inject
:
ar.inject({}) { |memo, subhash| subhash.each { |prod, value| memo[prod] ||= 0 ; memo[prod] += value } ; memo }
=> {"Dry Goods"=>5, "Vegetable"=>15}
さらに良いことに、あなたが使用する場合 Hash.new
デフォルト値は0です。
ar.inject(Hash.new(0)) { |memo, subhash| subhash.each { |prod, value| memo[prod] += value } ; memo }
=> {"Dry Goods"=>5, "Vegetable"=>15}
または場合 inject
頭を傷つけます:
result = Hash.new(0)
ar.each { |subhash| subhash.each { |prod, value| result[prod] += value } }
result
=> {"Dry Goods"=>5, "Vegetable"=>15}
私は各ハッシュに複数のエントリがないので、ハッシュがあなたがここで望むものであるかどうかはわかりません。それで、私はあなたのデータ表現を少し変更することから始めます。
ProductCount=Struct.new(:name,:count)
data = [ProductCount.new("Vegetable",10),
ProductCount.new("Vegetable",5),
ProductCount.new("Dry Goods",3),
ProductCount.new("Dry Goods",2)]
ハッシュが複数のキー価値ペアを持つことができる場合、おそらくやりたいことは
data = [{"Vegetable"=>10}, {"Vegetable"=>5}, {"Dry Goods"=>3>}, {"Dry Goods"=>2}]
data = data.map{|h| h.map{|k,v| ProductCount.new(k,v)}}.flatten
次のようにファセットの宝石を使用します
require 'facets'
data.group_by(&:name).update_values{|x| x.map(&:count).sum}
結果は次のとおりです
{"Dry Goods"=>5, "Vegetable"=>15}
複数のキーを備えた2つのハッシュがある場合:
h1 = { "Vegetable" => 10, "Dry Goods" => 2 }
h2 = { "Dry Goods" => 3, "Vegetable" => 5 }
details = {}
(h1.keys | h2.keys).each do |key|
details[key] = h1[key].to_i + h2[key].to_i
end
details
所属していません StackOverflow