开发者

Hash invert in Ruby?

开发者 https://www.devze.com 2023-04-06 12:08 出处:网络
I\'ve got a hash of the format: {key1 =&开发者_C百科gt; [a, b, c], key2 => [d, e, f]} and I want to end up with:

I've got a hash of the format:

{key1 =&开发者_C百科gt; [a, b, c], key2 => [d, e, f]}

and I want to end up with:

{ a => key1, b => key1, c => key1, d => key2 ... }

What's the easiest way of achieving this?

I'm using Ruby on Rails.

UPDATE

OK I managed to extract the real object from the server log, it is being pushed via AJAX.

  Parameters: {"status"=>{"1"=>["1", "14"], "2"=>["7", "12", "8", "13"]}}


hash = {:key1 => ["a", "b", "c"], :key2 => ["d", "e", "f"]}

first variant

hash.map{|k, v| v.map{|f| {f => k}}}.flatten
#=> [{"a"=>:key1}, {"b"=>:key1}, {"c"=>:key1}, {"d"=>:key2}, {"e"=>:key2}, {"f"=>:key2}] 

or

hash.inject({}){|h, (k,v)| v.map{|f| h[f] = k}; h}
#=> {"a"=>:key1, "b"=>:key1, "c"=>:key1, "d"=>:key2, "e"=>:key2, "f"=>:key2} 

UPD

ok, your hash is:

hash = {"status"=>{"1"=>["1", "14"], "2"=>["7", "12", "8", "13"]}}
hash["status"].inject({}){|h, (k,v)| v.map{|f| h[f] = k}; h}
#=> {"12"=>"2", "7"=>"2", "13"=>"2", "8"=>"2", "14"=>"1", "1"=>"1"}


Lots of other good answers. Just wanted to toss this one in too for Ruby 2.0 and 1.9.3:

hash = {apple: [1, 14], orange: [7, 12, 8, 13]}

Hash[hash.flat_map{ |k, v| v.map{ |i| [i, k] } }]
# => {1=>:apple, 14=>:apple, 7=>:orange, 12=>:orange, 8=>:orange, 13=>:orange}

This is leveraging: Hash::[] and Enumerable#flat_map

Also in these new versions there is Enumerable::each_with_object which is very similar to Enumerable::inject/Enumerable::reduce:

hash.each_with_object(Hash.new){ |(k, v), inverse|
  v.each{ |e| inverse[e] = k }
}

Performing a quick benchmark (Ruby 2.0.0p0; 2012 Macbook Air) using an original hash with 100 keys, each with 100 distinct values:

Hash::[] w/ Enumerable#flat_map
            155.7 (±9.0%) i/s -        780 in   5.066286s
Enumerable#each_with_object w/ Enumerable#each
            199.7 (±21.0%) i/s -        940 in   5.068926s

Shows that the each_with_object variant is faster for that data set.


Ok, let's guess. You say you have an array but I agree with Benoit that what you probably have is a hash. A functional approach:

 h = {:key1 => ["a", "b", "c"], :key2 => ["d", "e", "f"]}
 h.map { |k, vs| Hash[vs.map { |v| [v, k] }] }.inject(:merge)
 #=> {"a"=>:key1, "b"=>:key1, "c"=>:key1, "d"=>:key2, "e"=>:key2, "f"=>:key2}

Also:

 h.map { |k, vs| Hash[vs.product([k])] }.inject(:merge)
 #=> {"a"=>:key1, "b"=>:key1, "c"=>:key1, "d"=>:key2, "e"=>:key2, "f"=>:key2}


In the case where a value corresponds to more than one key, like "c" in this example...

{ :key1 => ["a", "b", "c"], :key2 => ["c", "d", "e"]}

...some of the other answers will not give the expected result. We will need the reversed hash to store the keys in arrays, like so:

{ "a" => [:key1], "b" => [:key1], "c" => [:key1, :key2], "d" => [:key2], "e" => [:key2] }

This should do the trick:

reverse = {}
hash.each{ |k,vs|
    vs.each{ |v|
        reverse[v] ||= []
        reverse[v] << k
    }
}

This was my use case, and I would have defined my problem much the same way as the OP (in fact, a search for a similar phrase got me here), so I suspect this answer may help other searchers.


If you're looking to reverse a hash formatted like this, the following may help you:

a = {:key1 => ["a", "b", "c"], :key2 => ["d", "e", "f"]}
a.inject({}) do |memo, (key, values)|
  values.each {|value| memo[value] = key }
  memo
end

this returns:

{"a"=>:key1, "b"=>:key1, "c"=>:key1, "d"=>:key2, "e"=>:key2, "f"=>:key2}


new_hash={}
hash = {"key1" => ['a', 'b', 'c'], "key2" => ['d','e','f']}
hash.each_pair{|key, val|val.each{|v| new_hash[v] = key }}

This gives

new_hash # {"a"=>"key1", "b"=>"key1", "c"=>"key1", "d"=>"key2", "e"=>"key2", "f"=>"key2"}


If you want to correctly deal with duplicate values, then you should use the Hash#inverse from Facets of Ruby

Hash#inverse preserves duplicate values, e.g. it ensures that hash.inverse.inverse == hash

either:

  • use Hash#inverse from here: http://www.unixgods.org/Ruby/invert_hash.html

  • use Hash#inverse from FacetsOfRuby library 'facets'

usage like this:

require 'facets'

h = {:key1 => [:a, :b, :c], :key2 => [:d, :e, :f]}
 => {:key1=>[:a, :b, :c], :key2=>[:d, :e, :f]} 

h.inverse
 => {:a=>:key1, :b=>:key1, :c=>:key1, :d=>:key2, :e=>:key2, :f=>:key2} 

The code looks like this:

# this doesn't looks quite as elegant as the other solutions here,
# but if you call inverse twice, it will preserve the elements of the original hash

# true inversion of Ruby Hash / preserves all elements in original hash
# e.g. hash.inverse.inverse ~ h

class Hash

  def inverse
    i = Hash.new
    self.each_pair{ |k,v|
      if (v.class == Array)
        v.each{ |x|
          i[x] = i.has_key?(x) ? [k,i[x]].flatten : k
        }
      else
        i[v] = i.has_key?(v) ? [k,i[v]].flatten : k
      end
    }
    return i
  end

end


h = {:key1 => [:a, :b, :c], :key2 => [:d, :e, :f]}
 => {:key1=>[:a, :b, :c], :key2=>[:d, :e, :f]} 

h.inverse
 => {:a=>:key1, :b=>:key1, :c=>:key1, :d=>:key2, :e=>:key2, :f=>:key2} 


One way to achieve what you're looking for:

arr = [{["k1"] => ["a", "b", "c"]}, {["k2"] => ["d", "e", "f"]}]

results_arr = []
arr.each do |hsh|
  hsh.values.flatten.each do |val|
    results_arr << { [val] => hsh.keys.first }···
  end
end


Result: [{["a"]=>["k1"]}, {["b"]=>["k1"]}, {["c"]=>["k1"]}, {["d"]=>["k2"]}, {["e"]=>["k2"]}, {["f"]=>["k2"]}]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号