Create Nested Hashes from a List of Hashes in Ruby

2019-08-14 04:54发布

问题:

I have a set of categories and their values stored as a list of hashes:

r = [{:A => :X}, {:A => :Y}, {:B => :X}, {:A => :X}, {:A => :Z}, {:A => :X},
     {:A => :X}, {:B => :Z}, {:C => :X}, {:C => :Y}, {:B => :X}, {:C => :Y},
     {:C => :Y}]

I'd like to get a count of each value coupled with its category as a hash like this:

{:A => {:X => 4, :Y => 1, :Z => 1},
 :B => {:X => 2, :Z => 1},
 :C => {:X => 1, :Y => 3}}

How can I do this efficiently?

Here's what I have so far (it returns inconsistent values):

r.reduce(Hash.new(Hash.new(0))) do |memo, x|
  memo[x.keys.first][x.values.first] += 1
  memo
end

Should I first compute the counts of all instances of specific {:cat => :val}s and then create the hash? Should I give a different base-case to reduce and change the body to check for nil cases (and assign zero when nil) instead of always adding 1?

EDIT:

I ended up changing my code and using the below method to have a cleaner way of achieving a nested hash:

r.map do |x|
  [x.keys.first, x.values.last]
end.reduce({}) do |memo, x|
  memo[x.first] = Hash.new(0) if memo[x.first].nil?
  memo[x.first][x.last] += 1
  memo
end

回答1:

The problem of your code is: memo did not hold the value. Use a variable outside the loop to hold the value would be ok:

memo = Hash.new {|h,k| h[k] = Hash.new {|hh, kk| hh[kk] = 0 } }

r.each do |x|
  memo[x.keys.first][x.values.first] += 1
end

p memo

And what's more, it won't work to init a hash nested inside a hash directly like this:

# NOT RIGHT
memo = Hash.new(Hash.new(0)) 
memo = Hash.new({})

Here is a link for more about the set default value issue: http://www.themomorohoax.com/2008/12/31/why-setting-the-default-value-of-a-hash-to-be-a-hash-is-wrong



回答2:

Not sure what "inconsistent values" means, but your problem is the hash you're injecting into is not remembering its results

r.each_with_object(Hash.new { |h, k| h[k] = Hash.new 0 }) do |individual, consolidated|
  individual.each do |key, value|
    consolidated[key][value] += 1
  end
end

But honestly, it would probably be better to just go to wherever you're making this array and change it to aggregate values like this.



回答3:

Functional approach using some handy abstractions -no need to reinvent the wheel- from facets:

require 'facets'    
r.map_by { |h| h.to_a }.mash { |k, vs| [k, vs.frequency] }
#=> {:A=>{:X=>4, :Y=>1, :Z=>1}, :B=>{:X=>2, :Z=>1}, :C=>{:X=>1, :Y=>3}}