ruby 将hash转换为hash数组

6ju8rftf  于 2023-11-18  发布在  Ruby
关注(0)|答案(6)|浏览(147)

我有哈希,它的所有值都是数组,像这样:

list = { letter:  ['a', 'b', 'c'],
         number:  ['one', 'two', 'three'],
         fruit:   ['apple', 'pear', 'kiwi'],
         car:     ['vw', 'mb', 'bmw'],
         state:   ['la', 'ny', 'fl'],
         color:   ['red', 'white', 'black'],
         tree:    ['oak', 'pine', 'maple'],
         animal:  ['cat', 'dog', 'rat'],
         clothes: ['tie', 'sock', 'glove'] }

字符串
事实上,这个哈希可以有更多的键,值可以更大,但每个值的大小总是相同的(在这种情况下-三个)。
我想把这个哈希值转换成哈希数组。
每个散列将具有原始散列的所有键和相应的值。
最后,我想拥有:

list = [
  { letter: 'a', number: 'one', fruit: 'apple', car: 'vw', state: 'la',
    color: 'red', tree: 'oak', animal: 'cat', clothes: 'tie' },

  { letter: 'b', number: 'two', fruit: 'pear', car: 'mb', state: 'ny',
    color: 'white', tree: 'pine', animal: 'dog', clothes: 'sock' },

  { letter: 'c', number: 'three', fruit: 'kiwi', car: 'bmw', state: 'fl',
    color: 'black', tree: 'elm', animal: 'rat', clothes: 'glove' }
]


最好的办法是什么?

pbpqsu0x

pbpqsu0x1#

使用Array::new作为新数组“构建器”,Enumerable#each_with_object作为每个项的“构建器”

Array.new(list.first.last.size) do |index|
  list.each_with_object({}) { |(key, values), new_item| new_item[key] = values[index] }
end

字符串

ssgvzors

ssgvzors2#

[list.keys].product(list.values.transpose).map { |a| a.transpose.to_h }
   #=> [{:letter=>"a", :number=>"one", :fruit=>"apple", :car=>"vw", 
   #     :state=>"la", :color=>"red", :tree=>"oak", :animal=>"cat",
   #     :clothes=>"tie"},
   #    {:letter=>"b", :number=>"two", :fruit=>"pear", :car=>"mb",
   #     :state=>"ny", :color=>"white", :tree=>"pine", :animal=>"dog",
   #     :clothes=>"sock"},
   #    {:letter=>"c", :number=>"three", :fruit=>"kiwi", :car=>"bmw",
   #     :state=>"fl", :color=>"black", :tree=>"maple", :animal=>"rat", 
   #     :clothes=>"glove"}]

字符串
假设list定义如下。

list = {
  letter:  ['a',     'b'   ],
  number:  ['one',   'two' ],
  fruit:   ['apple', 'pear'],
  car:     ['vw',    'mb'  ]
}


步骤如下。

b = [list.keys]
  #=> [[:letter, :number, :fruit, :car]]
c = list.values
  #=> [["a", "b"], ["one", "two"], ["apple", "pear"], ["vw", "mb"]]
d = c.transpose
  #=> [["a", "one", "apple", "vw"],
  #    ["b", "two", "pear",  "mb"]]
e = b.product(d)
  #=> [[[:letter, :number, :fruit, :car], ["a", "one", "apple", "vw"]],
  #    [[:letter, :number, :fruit, :car], ["b", "two", "pear",  "mb"]]]
e.map { |a| a.transpose.to_h }
  #=> [{:letter=>"a", :number=>"one", :fruit=>"apple", :car=>"vw"},
  #    {:letter=>"b", :number=>"two", :fruit=>"pear",  :car=>"mb"}]


让我们更仔细地看看最后一步。mape的第一个元素传递给块,并将块变量a设置为它的值:

a = e.first
  #=> [[:letter, :number,  :fruit,  :car],
  #    ["a",     "one",    "apple", "vw"]]


块计算如下。

f = a.transpose
  #=> [[:letter, "a"], [:number, "one"], [:fruit, "apple"], [:car,   "vw"]]
f.to_h
  #=> {:letter=>"a", :number=>"one", :fruit=>"apple", :car=>"vw"}


e.map { |a| a.transpose.to_h }的其余计算类似。

y3bcpkx1

y3bcpkx13#

利用Array#transpose和Array#to_h

keys = list.keys
list.values.transpose.map { |v| keys.zip(v).to_h }

字符串

roejwanj

roejwanj4#

All in one:

list.map{|k,v| [k].product(v)}.transpose.map(&:to_h)

字符串
这个想法是使用每个关键产品的值,Map,转置,然后通过to_h转换为散列。

bqf10yzr

bqf10yzr5#

尝试以下代码,

arr = []
3.times { |x| arr[x] = {}; list.each { |k,v| arr[x][k] = v[x] } }
arr.inspect

字符串
输出将

=> [{:letter=>"a", :number=>"one", :fruit=>"apple", :car=>"vw", :state=>"la", :color=>"red", :tree=>"oak", :animal=>"cat", :clothes=>"tie"}, {:letter=>"b", :number=>"two", :fruit=>"pear", :car=>"mb", :state=>"ny", :color=>"white", :tree=>"pine", :animal=>"dog", :clothes=>"sock"}, {:letter=>"c", :number=>"three", :fruit=>"kiwi", :car=>"bmw", :state=>"fl", :color=>"black", :tree=>"maple", :animal=>"rat", :clothes=>"glove"}]

jljoyd4f

jljoyd4f6#

关于Enumerable#each_with_object

list.values.transpose.each_with_object([]) { |v, a| a << v.each_with_index.with_object({}) { |(vv, i), h| h[list.keys[i]] = vv } }

字符串

相关问题