2, "idx"=>111, "money"=>"4.00", "money1"=>"1.00", "order"=>"001", "order1"=>"1"}, -6ren">
gpt4 book ai didi

ruby-on-rails - Ruby - 如何通过条件最大的多个键检索数组组中的总和

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

原数组为

[
{"id"=>2, "idx"=>111, "money"=>"4.00", "money1"=>"1.00", "order"=>"001", "order1"=>"1"},
{"id"=>1, "idx"=>112, "money"=>"2.00", "money1"=>"2.00", "order"=>"001", "order1"=>"1"},
{"id"=>3, "idx"=>113, "money"=>"3.00", "money1"=>"1.00", "order"=>"002", "order1"=>"2"}
]

预期的数组应该:

[
{"id"=>2, "idx"=>112, "money"=>"6.00", "money1"=>"3.00","order"=>"001", "order1"=>"1"},
{"id"=>3, "idx"=>113, "money"=>"3.00", "money1"=>"1.00","order"=>"002", "order1"=>"2"}
]

调用喜欢 f_addition(arr, group_fields, sum_fields, max_fields) (e.x: f_addition(arr, ["order","order1"], ["money","money1 "], ["id", "idx"] )

P/s: 调用方法后不要改变原来的(它再次用作新的group_fields,新的sum_fields,新的max_fields的参数)

主题Hash of Arrays, group by and sum by with many columns 中的类似问题

最佳答案

这样做的一种方法是使用 Hash#update 的形式(又名 merge! ),它使用一个 block 来确定要合并的两个散列中存在的键的值。

代码

def f_addition(arr, group_fields, sum_fields, max_fields)
arr.each_with_object({}) do |h,g|
g.update( h.values_at(*group_fields) => h ) do |_,gv,hv|
gv.merge(hv) do |k,gvv,hvv|
case
when sum_fields.include?(k) then "%.2f" % (gvv.to_f + hvv.to_f)
when max_fields.include?(k) then [gvv, hvv].max
else gvv
end
end
end
end.values
end

示例

arr = [{ "id"=>2, "idx"=>111, "money"=>"4.00", "money1"=>"1.00",
"order"=>"001", "order1"=>"1", "pet"=>"dog" },
{ "id"=>1, "idx"=>112, "money"=>"2.00", "money1"=>"2.00",
"order"=>"001", "order1"=>"1", "sport"=>"darts" },
{ "id"=>3, "idx"=>113, "money"=>"3.00", "money1"=>"1.00",
"order"=>"002", "order1"=>"2" }]

请注意,此数组与问题中给出的数组略有不同。我添加了 "pet"=>"dog"到第一个(散列)元素 "sport"=>"darts"和第二个哈希。

f_addition(arr, ["order","order1"], ["money","money1"], ["id", "idx"] )
#=> [{ "id"=>2, "idx"=>112, "money"=>"6.00", "money1"=>"3.00",
# "order"=>"001", "order1"=>"1", "pet"=>"dog", "sport"=>"darts"},
# { "id"=>3, "idx"=>113, "money"=>"3.00", "money1"=>"1.00",
# "order"=>"002", "order1"=>"2" }]

解释

对于上面的例子:

group_fields = ["order", "order1"]
sum_fields = ["money", "money1"]
max_fields = ["id", "idx"]

enum = arr.each_with_object({})
#=> #<Enumerator: [{"id"=>2, "idx"=>111,..., "pet"=>"dog"},
# {"id"=>1, "idx"=>112,..., "sport"=>"darts"},
# {"id"=>3,"idx"=>113,...,"order1"=>"2"}]:each_with_object({})>

Array#each将此枚举器的每个元素传递到 block 中并将其分配给 block 变量。传递的第一个元素是:

h, g = enum.next
#=> [{ "id"=>2, "idx"=>111, "money"=>"4.00", "money1"=>"1.00",
"order"=>"001", "order1"=>"1", "pet"=>"dog" }, {}]
h #=> { "id"=>2, "idx"=>111, "money"=>"4.00", "money1"=>"1.00",
"order"=>"001", "order1"=>"1", "pet"=>"dog" }
g #=> {}

作为:

h.values_at(*group_fields)
#=> h.values_at(*["order", "order1"])
#=> h.values_at("order", "order1")
#=> ["001", "1"]

我们计算:

g.update(["001", "1"] => h) do |k,gv,hv| ... end

这是以下内容的简写:

g.update({ ["001", "1"] => h }) do |k,gv,hv| ... end

区 block do |k,gv,hv| ... end仅在合并的两个散列均包含键 k 时使用.1 作为g = {}不包含 key ,此时不使用该 block :

g.update({ ["001", "1"] => h })
#=> {}.update({ ["001", "1"]=>{ "id"=>2, "idx"=>111, "money"=>"4.00",
# "money1"=>"1.00", "order"=>"001",
# "order1"=>"1", "pet"=>"dog" } }
#=> { ["001", "1"]=>{ "id"=>2, "idx"=>111, "money"=>"4.00", "money1"=>"1.00",
# "order"=>"001", "order1"=>"1", "pet"=>"dog" } }

update 返回的值在哪里是 g 的新值.

enum 的下一个值传递到 block 中的是:

h, g = enum.next
h #=> { "id"=>1, "idx"=>112, "money"=>"2.00", "money1"=>"2.00",
# "order"=>"001", "order1"=>"1", "sport"=>"darts" },
g #=> { ["001", "1"]=>{ "id"=>2, "idx"=>111, "money"=>"4.00", "money1"=>"1.00",
# "order"=>"001", "order1"=>"1", "pet"=>"dog" } }]

作为:

h.values_at(*group_fields)
#=> h.values_at("order", "order1")
#=> ["001", "1"]

我们计算:

g.update(["001", "1"] => h) do |k,gv,hv| ... end

作为g{ ["001", "1"] => h }两者都包含键 ["001", "1"],我们必须遵从 block 来确定合并哈希中该键的值。我们有:

k  = ["001", "1"]
gv = { "id"=>2, "idx"=>111, "money"=>"4.00", "money1"=>"1.00",
"order"=>"001", "order1"=>"1", "pet"=>"dog" }
hv = { "id"=>1, "idx"=>112, "money"=>"2.00", "money1"=>"2.00",
"order"=>"001", "order1"=>"1", "sport"=>"darts" }

因此,我们按如下方式评估 block (使用 merge 而不是 merge!/update ):

gv.merge(hv) do |k,gvv,hvv|
case
when sum_fields.include?(k) then "%.2f" % (gvv.to_f + hvv.to_f)
when max_fields.include?(k) then [gvv, hvv].max
else gvv
end
end
#=> { "id"=>2, "idx"=>112, "money"=>"6.00", "money1"=>"3.00",
# "order"=>"001", "order1"=>"1", "pet"=>"dog", "sport"=>"darts"}

gv不包含键“sport”,因此合并时不使用该 block "sport"=>"darts"进入gv . hvv 的所有其他键存在于 gvv , 然而,所以我们使用 block 来确定它们在合并哈希中的值。对于:

k = "money"
gvv = "4.00"
hvv = "2.00"

我们发现:

sum_fields.include?(k)
#=> ["money", "money1"].include?("money")
#=> true

所以case语句返回:

"%.2f" % (gvv.to_f + hvv.to_f)
#=> "%.2f" % ("4.00".to_f + "2.00".to_f)
#=> "6.00"

hv 的其他键值,哈希被合并到 gv , 计算类似,为我们提供合并哈希的新值 g .

最后,

{ ["002", "order1"] => { "id"=>3, "idx"=>113, "money"=>"3.00",
"money1"=>"1.00", "order"=>"002", "order1"=>"2" }]

合并到g (不需要使用 update 的 block )和 g.values由方法返回。

观察

很容易将其概括为传递对,例如:

[["money","money1"], ->(a,b) { "%.2f" % (a.to_f + b.to_f) }]
[["id", "idx"], :max]

这可以按如下方式完成:

def f_addition(arr, group_fields, *mods)
arr.each_with_object({}) do |h,g|
g.update( h.values_at(*group_fields) => h ) do |_,gv,hv|
gv.merge(hv) do |k,gvv,hvv|
f,op = mods.find { |f,op| f.include?(k) }
if f
case op
when Proc then op.call(gvv,hvv)
when Symbol then [gvv, hvv].send(op)
end
else
gvv
end
end
end
end.values
end

f_addition(arr, ["order","order1"],
[["money","money1"], ->(a,b) { "%.2f" % (a.to_f + b.to_f) }],
[["id", "idx"], :max])
# => [{ "id"=>2, "idx"=>112, "money"=>"6.00", "money1"=>"3.00",
# "order"=>"001", "order1"=>"1", "pet"=>"dog", "sport"=>"darts" },
# { "id"=>3, "idx"=>113, "money"=>"3.00", "money1"=>"1.00",
# "order"=>"002", "order1"=>"2" }]
1. We will find that the calculations in the block do not depend on the block variable `k`.   I've therefore replaced that variable with the local variable _, to so-inform the reader.

关于ruby-on-rails - Ruby - 如何通过条件最大的多个键检索数组组中的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28469348/

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