在Ruby中使用Hash反转?

时间:2011-09-22 11:04:17

标签: ruby arrays hash

我有一个格式的哈希:

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

我希望最终得到:

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

实现这一目标的最简单方法是什么?

我正在使用Ruby on Rails。

更新

好的,我设法从服务器日志中提取真实对象,它是通过AJAX推送的。

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

8 个答案:

答案 0 :(得分:7)

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

第一个变种

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

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

好吧,你的哈希是:

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"}

答案 1 :(得分:3)

很多其他好的答案。只是想为Ruby 2.0和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}

这是利用:Hash::[]Enumerable#flat_map

同样在这些新版本中,Enumerable::each_with_objectEnumerable::inject / Enumerable::reduce非常相似:

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

使用包含100个密钥的原始哈希执行快速benchmark(Ruby 2.0.0p0; 2012 Macbook Air),每个密钥具有100个不同的值:

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

显示该数据集的each_with_object变体更快。

答案 2 :(得分:2)

好的,我们猜猜看。你说你有一个数组,但我同意Benoit你可能拥有的是哈希。功能性方法:

 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}

此外:

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

答案 3 :(得分:2)

如果值对应多个键,例如本例中的“c”......

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

......其他一些答案不会给出预期的结果。我们需要反向散列来将键存储在数组中,如下所示:

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

这应该可以解决问题:

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

这是我的用例,我会像OP一样定义我的问题(事实上,搜索类似的短语让我在这里),所以我怀疑这个答案可能会帮助其他搜索者。

答案 4 :(得分:1)

如果您想要反转这样格式化的哈希值,以下内容可能会对您有所帮助:

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

返回:

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

答案 5 :(得分:1)

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

这给出了

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

答案 6 :(得分:1)

如果要正确处理重复值,则应使用Hash#inverse 来自Ruby的Facets

Hash#inverse保留重复值, 例如它确保hash.inverse.inverse == hash

或者:

这样的用法:

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} 

代码如下所示:

# 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} 

答案 7 :(得分:0)

实现目标的一种方法:

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"]}]