172

ハッシュのキーと値を交換するにはどうすればよいですか?

私は次のハッシュを持っています:

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

に変換したい:

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

使用mapはかなり退屈なようです。より短い解決策はありますか?

4

6 に答える 6

310

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
于 2012-06-12T01:08:53.967 に答える
65

あなたは1つあるに違いない!Ruby で物事を行うためのより短い方法が常にあります。

それは非常に簡単です。使用するだけHash#invertです:

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

ほら!

于 2012-06-12T00:46:16.170 に答える
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
于 2016-09-03T12:56:40.473 に答える
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
于 2015-04-14T11:12:50.453 に答える