在Ruby中,如何在Hash上交换键和值?
假设我有以下哈希:
{:a=>:one, :b=>:two, :c=>:three}
我想转变为:
{:one=>:a, :two=>:b, :three=>:c}
使用地图似乎相当乏味。是否有更短的解决方案?
答案 0 :(得分:247)
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}
<强> BUT ... 强>
如果您有重复值,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
答案 1 :(得分:61)
你打赌有一个!在Ruby中总是有一种更简单的方法!
这很简单,只需使用Hash#invert
:
{a: :one, b: :two, c: :three}.invert
=> {:one=>:a, :two=>:b, :three=>:c}
Etvoilà!
答案 2 :(得分: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 :(得分: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
答案 4 :(得分: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
答案 5 :(得分: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"]}