如何交换哈希中的键和值?
我有以下哈希:
{:a=>:one, :b=>:two, :c=>:three}
我想变成:
{:one=>:a, :two=>:b, :three=>:c}
使用
map
似乎相当乏味。有没有更短的解决方案?
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
class Hash
def safe_invert
self.each_with_object({}){|(k,v),o|(o[v]||=[])<<k}
end
end
这也将处理重复值。
:
> {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"]}
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
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
>> numstrs = %w(zero one two three four)
=> ["zero", "one", "two", "three", "four"]
>> numints = (0..4).to_a
=> [0, 1, 2, 3, 4]
>> combarr = numints.zip(numstrs)
=> [[0, "zero"], [1, "one"], [2, "two"], [3, "three"], [4, "four"]]
>> combhash = combarr.to_h
=> {0=>"zero", 1=>"one", 2=>"two", 3=>"three", 4=>"four"}
>> invhash = combhash.values.zip(combhash.keys).to_h
=> {"zero"=>0, "one"=>1, "two"=>2, "three"=>3, "four"=>4}