如何在哈希表中交换键和值

179

如何交换哈希表中的键和值?

我有以下的哈希表:

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

我想要转换成:

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

使用 map 似乎相当繁琐。是否有更简洁的解决方案?

6个回答

332

Ruby有一个用于Hash的帮助方法,可以让你把一个Hash当做是反向的(实质上,通过让你通过值来访问键):

{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

注意:带有测试的代码现在已经在GitHub上了。

或者:

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

4
在这里使用each_with_object比使用inject更合理。 - Andrew Marshall
每个对象都可以使用each_with_object({}){ |i,o|k,v = *i; o[v] ||=[]; o[v] << k}来实现,非常不错。 - Nigel Thorne
3
哦,我不知道你可以用|(key,value),out|来操作。这太棒了,以前我讨厌那个数组,它没有像键和值一样清晰明了。非常感谢! - Iuri G.

70

当然有!在Ruby中,总有一种更短的方法来完成事情!

非常简单,只需使用Hash#invert

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

就这样!


5
如果您的哈希表中出现重复的值,Hash#invert将无法正常工作。 - Tilo

3
files = {
  'Input.txt' => 'Randy',
  'Code.py' => 'Stan',
  'Output.txt' => 'Randy'
}

h = Hash.new{|h,k| h[k] = []} # Create hash that defaults unknown keys to empty an empty list
files.map {|k,v| h[v]<< k} #append each key to the list at a known value
puts h

这将处理重复的值。


1
你能简单解释一下每个步骤中发生了什么吗? - Sajjad Murtaza
个人而言,我避免设置哈希的默认值行为。我担心无论我给这个哈希什么代码,它都不会期望哈希以那种方式运作,这可能会在以后引起一些隐匿的错误。我无法真正证明这种担忧。这只是一个我似乎无法忽视的疑虑。最少惊讶原则? - Nigel Thorne
回答问题时,重要的是解释代码的工作原理以及为什么它是合适的解决方案。目标是教育,而不仅仅是解决眼前的问题。 - the Tin Man

2

如果你有一个唯一键的哈希表,可以使用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]}

如果哈希的值已经是数组,可以这样做: ```ruby 如果哈希的值已经是数组,可以这样做: ``` 请注意保留HTML标签。
> 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"]}

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
使用数组
input = {:key1=>"value1", :key2=>"value2", :key3=>"value3", :key4=>"value4", :key5=>"value5"}
output = Hash[input.to_a.map{|m| m.reverse}]

使用哈希表
input = {:key1=>"value1", :key2=>"value2", :key3=>"value3", :key4=>"value4", :key5=>"value5"}
output = input.invert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接