array[0..2], "key2" => array[0..3], "key-6ren">
gpt4 book ai didi

Ruby:在初始化哈希时减少重复

转载 作者:数据小太阳 更新时间:2023-10-29 08:25:05 25 4
gpt4 key购买 nike

array = [0, 0.3, 0.4, 0.2, 0.6]

hash = {
"key1" => array[0..2],
"key2" => array[0..3],
"key3" => array,
"key4" => array,
"key5" => array,
"key6" => array,
"key7" => array
}

有没有办法通过做类似的事情来删除重复

hash = {
"key1" => array[0..2],
"key2" => array[0..3],
%(key3, key4, key5, key6, key7).each {|ele| ele => array}
}

最佳答案

尝试

array = [0, 0.3, 0.4, 0.2, 0.6]
hash = {
"key1" => array[0..2],
"key2" => array[0..3]
}
%w(key3 key4 key5 key6 key7).each {|ele| hash[ele] = array}

关于Ruby:在初始化哈希时减少重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4964562/

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