2012-06-12 144 views
118

在Ruby中,如何交換哈希上的密鑰和值?在哈希中交換密鑰和值

比方說,我有以下散列:

{:a=>:one, :b=>:two, :c=>:three} 

,我想變成:

{:one=>:a, :two=>:b, :three=>:c} 

使用地圖顯得相當繁瑣。有一個更短的解決方案嗎?

回答

225

Ruby有一個散列幫助方法,可以讓你像對待散列那樣對待散列。

{a: 1, b: 2, c: 3}.key(1) 
=> :a 

如果你想保持倒散,然後Hash#invert應適用於大多數情況。

{a: 1, b: 2, c: 3}.invert 
=> {1=>:a, 2=>:b, 3=>:c} 

但是...

如果有重複的值,invert會丟棄所有,但最後你的價值觀。同樣key只會返回第一場比賽。

{a: 1, b: 2, c: 2}.key(2) 
=> :b 

{a: 1, b: 2, c: 2}.invert 
=> {1=>:a, 2=>:c} 

所以..如果你的價值觀是唯一可以使用Hash#invert如果沒有,那麼你就可以把所有的值作爲一個數組,像這樣:

class Hash 
    # like invert but not lossy 
    # {"one"=>1,"two"=>2, "1"=>1, "2"=>2}.inverse => {1=>["one", "1"], 2=>["two", "2"]} 
    def safe_invert 
    each_with_object({}) do |(key,value),out| 
     out[value] ||= [] 
     out[value] << key 
    end 
    end 
end 

注:此代碼與測試是現在here

或簡稱...

class Hash 
    def safe_invert 
    self.each_with_object({}){|(k,v),o|(o[v]||=[])<<k} 
    end 
end 
+0

非常相關!感謝您指出,我沒有測試這些情況。 –

+2

['each_with_object'](http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-each_with_object)在這裏比'inject'更有意義。 –

+0

這樣就變成了'each_with_object({}){| i,o | k,v = * i; o [v] || = []; o [v] << k}'...好的 –

58

你打賭有一個!在Ruby中做事總有一個更簡單的方法!

這很簡單,只需使用Hash#invert

{a: :one, b: :two, c: :three}.invert 
=> {:one=>:a, :two=>:b, :three=>:c} 

的Et瞧!

+2

如果相同的值在您的散列中多次出現,則散列#顛倒不起作用。 – Tilo

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

Hash#inverse爲您提供:

h = {a: 1, b: 2, c: 2} 
h.inverse 
    => {1=>:a, 2=>[:c, :b]} 
h.inverse.inverse 
    => {:a=>1, :c=>2, :b=>2} # order might not be preserved 
h.inverse.inverse == h 
    => true     # true-ish because order might change 

而內置invert方法剛剛打破:

h.invert 
    => {1=>:a, 2=>:c} # FAIL 
h.invert.invert == h 
    => false    # FAIL 
+1

這是迄今爲止最好的答案。 – kwerle

1

使用陣列

input = {:key1=>"value1", :key2=>"value2", :key3=>"value3", :key4=>"value4", :key5=>"value5"} 
output = Hash[input.to_a.map{|m| m.reverse}] 

使用Hash

input = {:key1=>"value1", :key2=>"value2", :key3=>"value3", :key4=>"value4", :key5=>"value5"} 
output = input.invert 
1
files = { 
    'Input.txt' => 'Randy', 
    'Code.py' => 'Stan', 
    'Output.txt' => 'Randy' 
} 

h = Hash.new{|h,k| h[k] = []} 
files.map {|k,v| h[v]<< k} 
puts h 

這將處理重複值了。

0

如果你有一個散列結果,其中的關鍵是是唯一的,你可以使用Hash#invert

> {a: 1, b: 2, c: 3}.invert 
=> {1=>:a, 2=>:b, 3=>:c} 

這不會,如果你有非唯一鍵,工作然而,在只看到最後一個鍵會不停:

> {a: 1, b: 2, c: 3, d: 3, e: 2, f: 1}.invert 
=> {1=>:f, 2=>:e, 3=>:d} 

如果您有非唯一鍵的哈希,你可以這樣做:

> hash={a: 1, b: 2, c: 3, d: 3, e: 2, f: 1} 
> hash.each_with_object(Hash.new { |h,k| h[k]=[] }) {|(k,v), h| 
      h[v] << k 
      }  
=> {1=>[:a, :f], 2=>[:b, :e], 3=>[:c, :d]} 

如果散列值已經是數組,則可以這樣做:

> hash={ "A" => [14, 15, 16], "B" => [17, 15], "C" => [35, 15] } 
> hash.each_with_object(Hash.new { |h,k| h[k]=[] }) {|(k,v), h| 
      v.map {|t| h[t] << k} 
      } 
=> {14=>["A"], 15=>["A", "B", "C"], 16=>["A"], 17=>["B"], 35=>["C"]}