我们有以下数据结构:
{:a => ["val1", "val2"], :b => ["valb1", "valb2"], ...}
我想将其转换成
[{:a => "val1", :b => "valb1"}, {:a => "val2", :b => "valb2"}, ...]
然后回到第一种形式。 任何人只要有一个好看的实施?
我们有以下数据结构:
{:a => ["val1", "val2"], :b => ["valb1", "valb2"], ...}
我想将其转换成
[{:a => "val1", :b => "valb1"}, {:a => "val2", :b => "valb2"}, ...]
然后回到第一种形式。 任何人只要有一个好看的实施?
该解决方案可与值(VAL1,VAL2 ... VALN)任意数字:
{:a => ["val1", "val2"], :b => ["valb1", "valb2"]}.inject([]){|a, (k,vs)|
vs.each_with_index{|v,i| (a[i] ||= {})[k] = v}
a
}
# => [{:a=>"val1", :b=>"valb1"}, {:a=>"val2", :b=>"valb2"}]
[{:a=>"val1", :b=>"valb1"}, {:a=>"val2", :b=>"valb2"}].inject({}){|a, h|
h.each_pair{|k,v| (a[k] ||= []) << v}
a
}
# => {:a=>["val1", "val2"], :b=>["valb1", "valb2"]}
使用功能的做法(见可枚举):
hs = h.values.transpose.map { |vs| h.keys.zip(vs).to_h }
#=> [{:a=>"val1", :b=>"valb1"}, {:a=>"val2", :b=>"valb2"}]
然后回来:
h_again = hs.first.keys.zip(hs.map(&:values).transpose).to_h
#=> {:a=>["val1", "val2"], :b=>["valb1", "valb2"]}
让我们来仔细看一下数据结构,我们正在努力之间的转换:
#Format A
[
["val1", "val2"], :a
["valb1", "valb2"], :b
["valc1", "valc2"] :c
]
#Format B
[ :a :b :c
["val1", "valb1", "valc1"],
["val2", "valb2", "valc3"]
]
它不diffculty找到Format B
是的转置Format A
本质性的,那么我们就可以拿出这个解决方案:
h={:a => ["vala1", "vala2"], :b => ["valb1", "valb2"], :c => ["valc1", "valc2"]}
sorted_keys = h.keys.sort_by {|a,b| a.to_s <=> b.to_s}
puts sorted_keys.inject([]) {|s,e| s << h[e]}.transpose.inject([]) {|r, a| r << Hash[*sorted_keys.zip(a).flatten]}.inspect
#[{:b=>"valb1", :c=>"valc1", :a=>"vala1"}, {:b=>"valb2", :c=>"valc2", :a=>"vala2"}]
m = {}
a,b = Array(h).transpose
b.transpose.map { |y| [a, y].transpose.inject(m) { |m,x| m.merge Hash[*x] }}
我尝试,或许更为简洁。
h = { :a => ["val1", "val2"], :b => ["valb1", "valb2"] }
h.values.transpose.map { |s| Hash[h.keys.zip(s)] }
如果用Ruby 1.9.3或更高版本。
首先,“结合”的相应的数值,“行”
h.values.transpose
# => [["val1", "valb1"], ["val2", "valb2"]]
在每次迭代map
块将产生下列操作之一:
h.keys.zip(s)
# => [[:a, "val1"], [:b, "valb1"]]
和Hash[]
将他们变成哈希:
Hash[h.keys.zip(s)]
# => {:a=>"val1", :b=>"valb1"} (for each iteration)
这将工作假设所有原散列大小相同的阵列:
hash_array = hash.first[1].map { {} }
hash.each do |key,arr|
hash_array.zip(arr).each {|inner_hash, val| inner_hash[key] = val}
end
你可以使用inject
建立散列的数组。
hash = { :a => ["val1", "val2"], :b => ["valb1", "valb2"] }
array = hash.inject([]) do |pairs, pair|
pairs << { pair[0] => pair[1] }
pairs
end
array.inspect # => "[{:a=>["val1", "val2"]}, {:b=>["valb1", "valb2"]}]"
Ruby文档有几个例子有工作的inject
。