我如何组和嵌套哈希和数组有相同的键添加值?

时间:2019-02-02 07:10:18

标签: arrays ruby hash

我正在尝试在哈希和数组的组合中获取每个学生的分数和平均成绩的总和,但是我的所有尝试仅返回所有条目的总和。有什么想法吗?

student_data = 
  {"ST4"=>[{:student_id=>"ST4", :points=> 5, :grade=>5}, 
           {:student_id=>"ST4", :points=>10, :grade=>4}, 
           {:student_id=>"ST4", :points=>20, :grade=>5}], 
   "ST1"=>[{:student_id=>"ST1", :points=>10, :grade=>3}, 
           {:student_id=>"ST1", :points=>30, :grade=>4}, 
           {:student_id=>"ST1", :points=>45, :grade=>2}], 
   "ST2"=>[{:student_id=>"ST2", :points=>25, :grade=>5}, 
           {:student_id=>"ST2", :points=>15, :grade=>1}, 
           {:student_id=>"ST2", :points=>35, :grade=>3}], 
   "ST3"=>[{:student_id=>"ST3", :points=> 5, :grade=>5}, 
           {:student_id=>"ST3", :points=>50, :grade=>2}]}

3 个答案:

答案 0 :(得分:1)

这样就可以获得所需的哈希值。

student_data.transform_values do |arr|
  points, grades = arr.map { |h| h.values_at(:points, :grade) }.transpose
  { :points=>points.sum, :grades=>grades.sum.fdiv(grades.size) }
end
  #=> {"ST4"=>{:points=>35, :grades=>4.666666666666667},
  #    "ST1"=>{:points=>85, :grades=>3.0},
  #    "ST2"=>{:points=>75, :grades=>3.0},
  #    "ST3"=>{:points=>55, :grades=>3.5}} 

传递给块的第一个值是第一个键'ST4'的值,并为块变量arr分配该值:

a = student_data.first
  #=> ["ST4",
  #    [{:student_id=>"ST4", :points=> 5, :grade=>5},
  #     {:student_id=>"ST4", :points=>10, :grade=>4},
  #     {:student_id=>"ST4", :points=>20, :grade=>5}]
  #   ] 
arr = a.last
  #=> [{:student_id=>"ST4", :points=> 5, :grade=>5},
  #    {:student_id=>"ST4", :points=>10, :grade=>4},
  #    {:student_id=>"ST4", :points=>20, :grade=>5}]

块计算如下。 arr传递给内部块的map的第一个值是

h = arr.first
  #=> {:student_id=>"ST4", :points=>5, :grade=>5} 
h.values_at(:points, :grade)
  #=> [5, 5] 

arr的其余两个元素传递到我们拥有的块中

b = arr.map { |h| h.values_at(:points, :grade) }
  #=> [[5, 5], [10, 4], [20, 5]] 

然后

points, grades = b.transpose
  #=> [[5, 10, 20], [5, 4, 5]] 
points
  #=> [5, 10, 20] 
grades
  #=> [5, 4, 5] 

我们现在仅形成'ST4'的哈希值。

c = points.sum
  #=> 35 
d = grades.sum
  #=> 14 
e = grades.size
  #=> 3 
f = c.fdiv(d)
  #=> 4.666666666666667 

'ST4'student_data的值因此映射到哈希值

{ :points=>c, :grades=>f }
  #=> {:points=>35, :grades=>4.666666666666667} 

student_data其余键的映射以类似方式计算。

请参见Hash#transform_valuesEnumerable#mapHash#values_atArray#transposeArray#sumInteger#fdiv

答案 1 :(得分:0)

您可以通过以下任何方式实现期望,

student_data.values.map do |z|
  z.group_by { |x| x[:student_id] }.transform_values do |v|
    { 
      points: v.map { |x| x[:points] }.sum, # sum of points
      grade: (v.map { |x| x[:grade] }.sum/v.count.to_f).round(2) # average of grades
    }
  end
end

由于未指定确切的预期输出格式,因此可以通过以下方式获得

=> [
  {"ST4"=>{:points=>35, :grade=>4.67}},
  {"ST1"=>{:points=>85, :grade=>3.0}},
  {"ST2"=>{:points=>75, :grade=>3.0}},
  {"ST3"=>{:points=>55, :grade=>3.5}}
]

答案 2 :(得分:0)

对于 Ruby 2.6 ,对于{strong> Ruby 2.5

使用Object#thenObject#yield_self
student_data.transform_values { |st| st
  .each_with_object(Hash.new(0)) { |h, hh|  hh[:sum_points] += h[:points]; hh[:sum_grade] += h[:grade]; hh[:count] += 1.0 }
  .then{ |hh| {tot_points: hh[:sum_points], avg_grade: hh[:sum_grade]/hh[:count] } }
}


它如何工作?

给出每个学生的数组:

st = [{:student_id=>"ST4", :points=> 5, :grade=>5}, {:student_id=>"ST4", :points=>10, :grade=>4}, {:student_id=>"ST4", :points=>20, :grade=>5}]

首先使用Enumerable#each_with_objectHash#default设置为零(Hash.new(0)来构建使用Hash#transform_values进行哈希添加和计数的哈希

step1 = st.each_with_object(Hash.new(0)) { |h, hh|  hh[:sum_points] += h[:points]; hh[:sum_grade] += h[:grade]; hh[:count] += 1.0 }
#=> {:sum_points=>35, :sum_grade=>14, :count=>3.0}

然后使用! (对于Ruby 2.5,{yield_self

step2 = step1.then{ |hh| {tot_points: hh[:sum_points], avg_grade: hh[:sum_grade]/hh[:count] }}
#=> {:tot_points=>35, :avg_grade=>4.666666666666667}

使用第一行代码issue将所有内容放在一起