gpt4 book ai didi

ruby - 数组到数组的哈希

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:29 24 4
gpt4 key购买 nike

我有一个从表中获得的值数组:

arr = ["One", "0", "31.948", "0", "6.94",
"Two", "0", "31.948", "0", "6.94",
"Three", "0", "23.961", "0", "5.21"]

我需要得到数组的散列:

hash = {
"One" => ["0", "31.948", "0", "6.94"],
"Two" => ["0", "31.948", "0", "6.94"],
"Three" => ["0", "23.961", "0", "5.21"]
}

我该怎么做?

最佳答案

假设您想要为每个值创建一个数组,这就可以了。

Hash[arr.each_slice(5).map{|k, *v| [k, v]}]

关于ruby - 数组到数组的哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12945775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com