我已经找到了解决我在JS中完成的问题的解决方案,但我需要在Ruby(RoR)中完成。以下是问题和解决方案的链接:Find average value for array of hashes using multiple group by
所以我有一系列哈希需要按键分组(先subject_id
然后element_id
),然后找到它们的平均值。数组中的哈希数不固定。
下面是输入数组:
a=[
{:subject_id=>1, :element_id=>2, :value=>55},
{:subject_id=>1, :element_id=>4, :value=>33},
{:subject_id=>1, :element_id=>2, :value=>33},
{:subject_id=>1, :element_id=>4, :value=>1},
{:subject_id=>1, :element_id=>2, :value=>7},
{:subject_id=>1, :element_id=>4, :value=>4},
{:subject_id=>2, :element_id=>2, :value=>3},
{:subject_id=>2, :element_id=>2, :value=>5},
{:subject_id=>2, :element_id=>4, :value=>9}
]
结果:
b=[
{:subject_id=>1, :element_id=>2, :value=>95},
{:subject_id=>1, :element_id=>4, :value=>38},
{:subject_id=>2, :element_id=>2, :value=>8},
{:subject_id=>2, :element_id=>4, :value=>9}
]
答案 0 :(得分:0)
我建议使用计算哈希来获取密钥:value
的小计,然后从该哈希构造所需的哈希数组。这使用Hash#new的形式,它接受一个参数,即哈希的默认值。这意味着如果哈希h
没有密钥k
,h[k]
将返回默认值。
计算总计
a.each_with_object(Hash.new(0)) {|g,h| h[[g[:subject_id], g[:element_id]]] += g[:value]}.
map {|(sub, el), tot| { subject_id: sub, element_id: el, value: tot}}
#=> [{:subject_id=>1, :element_id=>2, :value=>95},
# {:subject_id=>1, :element_id=>4, :value=>38},
# {:subject_id=>2, :element_id=>2, :value=>8},
# {:subject_id=>2, :element_id=>4, :value=>9}]
Ruby,作为第一步,解压缩表达式
h[[g[:subject_id], g[:element_id]]] += g[:value]
将其更改为
h[[g[:subject_id], g[:element_id]]] = h[[g[:subject_id], g[:element_id]]] + g[:value]
如果h
没有密钥[g[:subject_id], g[:element_id]]
,则等式右侧的h[[g[:subject_id], g[:element_id]]]
会返回默认值0
。
请注意
a.each_with_object(Hash.new(0)) {|g,h| h[[g[:subject_id], g[:element_id]]] += g[:value]}
#=> {[1, 2]=>95, [1, 4]=>38, [2, 2]=>8, [2, 4]=>9}
计算平均值
计算平均值只需要很小的改动。
a.each_with_object({}) do |g,h|
pair = [g[:element_id], g[:subject_id]]
h[pair] = {tot: 0, count: 0} unless h.key?(pair)
h[pair] = {tot: h[pair][:tot] + g[:value], count: h[pair][:count]+1}
end.map {|(sub, el),h| {subject_id: sub, element_id: el,
average: (h[:tot].to_f/h[:count]).round(1)}}
#=> [{:subject_id=>2, :element_id=>1, :average=>31.7},
# {:subject_id=>4, :element_id=>1, :average=>12.7},
# {:subject_id=>2, :element_id=>2, :average=>4.0},
# {:subject_id=>4, :element_id=>2, :average=>9.0}]
请注意
a.each_with_object({}) do |g,h|
pair = [g[:element_id], g[:subject_id]]
h[pair] = {tot: 0, count: 0} unless h.key?(pair)
h[pair] = {tot: h[pair][:tot] + g[:value], count: h[pair][:count]+1}
end
#=> {[2, 1]=>{:tot=>95, :count=>3}, [4, 1]=>{:tot=>38, :count=>3},
# [2, 2]=>{:tot=> 8, :count=>2}, [4, 2]=>{:tot=> 9, :count=>1}}
答案 1 :(得分:0)
问题中显示的结果不是平均值,而是总和,因此结果会有所不同:
def groupByAndAverage(a)
b = []
a.each_with_index do |element, key|
index = b.index do |x|
x != element &&
x[:subject_id] == element[:subject_id] &&
x[:element_id] == element[:element_id]
end
if index
b[index][:value] += element[:value]
b[index][:amount] += 1
else
b.push a[key].merge(amount: 1)
end
true
end
b.map do |element|
element[:value] = element[:value] / element[:amount]
element.delete(:amount)
element
end
b
end
结果是:
[{:subject_id=>1, :element_id=>2, :value=>31},
{:subject_id=>1, :element_id=>4, :value=>12},
{:subject_id=>2, :element_id=>2, :value=>4},
{:subject_id=>2, :element_id=>4, :value=>9}]