在Ruby中使用Hash反转?

问题描述 投票:6回答:8

我有一个格式的哈希:

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

我想最终得到:

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

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

我正在使用Ruby on Rails。

UPDATE

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

  Parameters: {"status"=>{"1"=>["1", "14"], "2"=>["7", "12", "8", "13"]}}
ruby arrays hash
8个回答
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"}

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_object,它与Enumerable::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
投票

好吧,我们猜。你说你有一个数组,但我同意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}

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一样定义我的问题(事实上,搜索类似的短语让我在这里),所以我怀疑这个答案可能会帮助其他搜索者。


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}

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

1
投票

如果你想正确处理重复值,那么你应该使用Ruby的Facets中的Hash#inverse

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} 

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"]}]
© www.soinside.com 2019 - 2024. All rights reserved.