【问题标题】:How to swap keys and values in a hash如何交换散列中的键和值
【发布时间】:2012-06-14 21:33:49
【问题描述】:

如何在 Hash 中交换键和值?

我有以下哈希:

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

我想转变成:

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

使用map 似乎相当乏味。有没有更短的解决方案?

【问题讨论】:

    标签: ruby hashmap


    【解决方案1】:

    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
    

    注意:这个带有测试的代码现在是on GitHub

    或者:

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

    【讨论】:

    • each_with_object 在这里比inject 更有意义。
    • 这样就变成了each_with_object({}){ |i,o|k,v = *i; o[v] ||=[]; o[v] &lt;&lt; k} ...不错
    • 天哪。我不知道你能做到 |(key,value),out|。太棒了,我讨厌那个数组而不是键和值。非常感谢
    【解决方案2】:
    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
    

    这也将处理重复值。

    【讨论】:

    • 你能解释一下每个步骤发生了什么吗?
    • 我个人避免设置散列的默认值行为。我担心我给这个散列的任何代码都不会期望散列以这种方式运行,并且以后可能会导致一些隐蔽的错误。我真的无法证明这种担忧是合理的。这只是一个我似乎无法忽视的疑问。最小意外原则?
    • 在回答代码时,解释代码的工作原理以及为什么它是合适的解决方案非常重要。目标是教育,而不仅仅是解决眼前的问题。
    【解决方案3】:

    如果您有一个哈希值,其中的键是唯一的,您可以使用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"]}
    

    【讨论】:

      【解决方案4】:

      使用数组

      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
      

      【讨论】:

        【解决方案5】:
        # 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
        

        【讨论】:

          【解决方案6】:

          你敢打赌有一个!在 Ruby 中做事总是有更短的方法!

          很简单,用Hash#invert:

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

          等等!

          【讨论】:

          • 如果相同的值在您的哈希中多次出现,Hash#invert 不起作用。
          猜你喜欢
          • 1970-01-01
          • 2015-11-21
          • 2016-11-04
          • 1970-01-01
          • 2021-11-03
          • 1970-01-01
          • 1970-01-01
          • 2013-07-19
          • 1970-01-01
          相关资源
          最近更新 更多