gpt4 book ai didi

ruby-on-rails - 用散列对数组求和

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

如何在这两个哈希数组之间执行加法,例如我想从第一个数组中获取第一个哈希并将 number, s_number, l_number 与第二个数组中的第一个哈希相加数组,总和将取决于 created_at,如果第一个数组中的 created_at 与第二个数组中的 created_at 相等,则执行 +。请查看我希望返回的内容,因为我不知道如何更好地解释这一点。

谢谢

raw_data = [
[
{"created_at"=>"2013-11-17","number"=>0,"s_number"=>0,"l_number"=>0},
{"created_at"=>"2013-11-18","number"=>6,"s_number"=>1,"l_number"=>4},
{"created_at"=>"2013-11-19","number"=>1,"s_number"=>1,"l_number"=>1},
{"created_at"=>"2013-11-20","number"=>0,"s_number"=>0,"l_number"=>0}
],
[
{"created_at"=>"2013-11-17","number"=>2,"s_number"=>3,"l_number"=>4},
{"created_at"=>"2013-11-18","number"=>1,"s_number"=>41,"l_number"=>1},
{"created_at"=>"2013-11-19","number"=>5,"s_number"=>45,"l_number"=>7},
{"created_at"=>"2013-11-20","number"=>7,"s_number"=>8,"l_number"=>2}
]
]

应该返回什么

[
{"created_at"=>"2013-11-17","number"=>2,"s_number"=>3,"l_number"=>4},
{"created_at"=>"2013-11-18","number"=>7,"s_number"=>42,"l_number"=>5},
{"created_at"=>"2013-11-19","number"=>6,"s_number"=>46"l_number"=>8},
{"created_at"=>"2013-11-20","number"=>7,"s_number"=>8,"l_number"=>2}
]

更新哈希格式与我发布的不同:

{:created_at=>Sun, 17 Nov 2013, :number=>0, :s_number=>0, :l_number=>0}

最佳答案

首先你需要对数组求和:

raw_data.sum

这将返回如下内容:

    [
{"created_at"=>"2013-11-17","number"=>0,"s_number"=>0,"l_number"=>0},
{"created_at"=>"2013-11-18","number"=>6,"s_number"=>1,"l_number"=>4},
{"created_at"=>"2013-11-19","number"=>1,"s_number"=>1,"l_number"=>1},
{"created_at"=>"2013-11-20","number"=>0,"s_number"=>0,"l_number"=>0}
{"created_at"=>"2013-11-17","number"=>2,"s_number"=>3,"l_number"=>4},
{"created_at"=>"2013-11-18","number"=>1,"s_number"=>41,"l_number"=>1},
{"created_at"=>"2013-11-19","number"=>5,"s_number"=>45,"l_number"=>7},
{"created_at"=>"2013-11-20","number"=>7,"s_number"=>8,"l_number"=>2}
]

现在,我们需要按“created_at”键对元素进行分组:

raw_data.sum.group_by{|element| element['created_at']}

这将导致:

{
"2013-11-17" => [
{"created_at"=>"2013-11-17","number"=>0,"s_number"=>0,"l_number"=>0},
{"created_at"=>"2013-11-17","number"=>2,"s_number"=>3,"l_number"=>4},
],
"2013-11-18" => [
{"created_at"=>"2013-11-18","number"=>6,"s_number"=>1,"l_number"=>4},
{"created_at"=>"2013-11-18","number"=>1,"s_number"=>41,"l_number"=>1}
], ...
}

我们只需要这个散列的值:

raw_data.sum.group_by{|element| element['created_at']}.values

# =>
[
[
{"created_at"=>"2013-11-17","number"=>0,"s_number"=>0,"l_number"=>0},
{"created_at"=>"2013-11-17","number"=>2,"s_number"=>3,"l_number"=>4},
],
[
{"created_at"=>"2013-11-18","number"=>6,"s_number"=>1,"l_number"=>4},
{"created_at"=>"2013-11-18","number"=>1,"s_number"=>41,"l_number"=>1}
], ...
}

现在我们需要将每个散列数组映射到一个散列。为此,我们将使用带有 block 的注入(inject)和合并功能:

raw_data.sum.group_by{|element| element[:created_at]}.values.map{|a| a.inject({}) {|result, hash| result.merge(hash) {|key, old_value, new_value| key == :created_at ? old_value : old_value + new_value}}}



# =>

[
{"created_at"=>"2013-11-17","number"=>2,"s_number"=>3,"l_number"=>4},
{"created_at"=>"2013-11-18","number"=>7,"s_number"=>42,"l_number"=>5},
{"created_at"=>"2013-11-19","number"=>6,"s_number"=>46"l_number"=>8},
{"created_at"=>"2013-11-20","number"=>7,"s_number"=>8,"l_number"=>2}
]

关于ruby-on-rails - 用散列对数组求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20094170/

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