如何在散列哈希中执行数字划分?

时间:2017-11-20 01:18:19

标签: ruby-on-rails ruby

我有关于学生参加体育课每小时的数据。

我需要按0和1的比例标准化这些数据。 所以我试图将distribution_by_place除以total_distribution

有没有一种很好的方法来执行此操作?

distribution_by_place的样本

{["cardio", "00"]=>0,
 ["cardio", "01"]=>0,
 ["cardio", "02"]=>0,
 ["cardio", "03"]=>0,
 ["cardio", "04"]=>0,
 ["cardio", "05"]=>0,
 ["cardio", "06"]=>0,
 ["cardio", "07"]=>0,
 ["cardio", "08"]=>0,
 ["cardio", "09"]=>0,
 ["cardio", "10"]=>0,
 ["cardio", "11"]=>0.35e2,
 ["cardio", "12"]=>0,
 ["cardio", "13"]=>0,
 ["cardio", "14"]=>0,
 ["cardio", "15"]=>0,
 ["cardio", "16"]=>0,
 ["cardio", "17"]=>0,
 ["cardio", "18"]=>0,
 ["cardio", "19"]=>0,
 ["cardio", "20"]=>0,
 ["cardio", "21"]=>0,
 ["cardio", "22"]=>0,
 ["cardio", "23"]=>0,
 ["swimming", "00"]=>0,
 ["swimming", "01"]=>0,
 ["swimming", "02"]=>0,
 ["swimming", "03"]=>0,
 ["swimming", "04"]=>0,
 ["swimming", "05"]=>0,
 ["swimming", "06"]=>0,
 ["swimming", "07"]=>0,
 ["swimming", "08"]=>0,
 ["swimming", "09"]=>0,
 ["swimming", "10"]=>0,
 ["swimming", "11"]=>0.35e2,
 ["swimming", "12"]=>0,
 ["swimming", "13"]=>0.3e2,
 ["swimming", "14"]=>0,
 ["swimming", "15"]=>0,
 ["swimming", "16"]=>0,
 ["swimming", "17"]=>0,
 ["swimming", "18"]=>0,
 ["swimming", "19"]=>0,
 ["swimming", "20"]=>0,
 ["swimming", "21"]=>0,
 ["swimming", "22"]=>0,
 ["swimming", "23"]=>0}

total_distribution样本

{"00"=>0,
 "01"=>0,
 "02"=>0,
 "03"=>0,
 "04"=>0,
 "05"=>0,
 "06"=>0,
 "07"=>0,
 "08"=>0,
 "09"=>0,
 "10"=>0,
 "11"=>0.7e2,
 "12"=>0,
 "13"=>0.3e2,
 "14"=>0,
 "15"=>0,
 "16"=>0,
 "17"=>0,
 "18"=>0,
 "19"=>0,
 "20"=>0,
 "21"=>0,
 "22"=>0,
 "23"=>0}

预期结果

{["cardio", "00"]=>0,
 ["cardio", "01"]=>0,
 ["cardio", "02"]=>0,
 ["cardio", "03"]=>0,
 ["cardio", "04"]=>0,
 ["cardio", "05"]=>0,
 ["cardio", "06"]=>0,
 ["cardio", "07"]=>0,
 ["cardio", "08"]=>0,
 ["cardio", "09"]=>0,
 ["cardio", "10"]=>0,
 ["cardio", "11"]=>0.5,
 ["cardio", "12"]=>0,
 ["cardio", "13"]=>0,
 ["cardio", "14"]=>0,
 ["cardio", "15"]=>0,
 ["cardio", "16"]=>0,
 ["cardio", "17"]=>0,
 ["cardio", "18"]=>0,
 ["cardio", "19"]=>0,
 ["cardio", "20"]=>0,
 ["cardio", "21"]=>0,
 ["cardio", "22"]=>0,
 ["cardio", "23"]=>0,
 ["swimming", "00"]=>0,
 ["swimming", "01"]=>0,
 ["swimming", "02"]=>0,
 ["swimming", "03"]=>0,
 ["swimming", "04"]=>0,
 ["swimming", "05"]=>0,
 ["swimming", "06"]=>0,
 ["swimming", "07"]=>0,
 ["swimming", "08"]=>0,
 ["swimming", "09"]=>0,
 ["swimming", "10"]=>0,
 ["swimming", "11"]=>0,
 ["swimming", "12"]=>0,
 ["swimming", "13"]=>1,
 ["swimming", "14"]=>0,
 ["swimming", "15"]=>0,
 ["swimming", "16"]=>0,
 ["swimming", "17"]=>0,
 ["swimming", "18"]=>0,
 ["swimming", "19"]=>0,
 ["swimming", "20"]=>0,
 ["swimming", "21"]=>0,
 ["swimming", "22"]=>0,
 ["swimming", "23"]=>0}

这些是函数

def hourly_periods
  HourlyPeriod
    .where(location_id: location.id)
    .where("start_at >= ? and end_at <= ?", start_date, end_date)
    .where.not(corrupted: true)
end

def distribution_by_place
  hourly_periods.includes(:place_type)
    .where.not(place_type_id: gym_entrance.id)
    .group("place_types.name")
    .group_by_hour_of_day(:start_at, format: "%H")
    .average(:attendance)
end

def total_distribution
  hourly_periods.includes(:place_type)
    .where.not(place_type_id: gym_entrance.id)
    .group_by_hour_of_day(:start_at, format: "%H")
    .average(:attendance)
end

1 个答案:

答案 0 :(得分:0)

忽略你的功能,只是看看你已经证明你可以这样做的示例哈希:

distribution_by_place.each do |key, value|
  index = key.second
  distribution_by_place[key] = value / total_distribution[index] unless total_distribution[index] == 0
end