查找具有公共密钥的哈希数组的最大值,最小值和平均值?

时间:2015-12-08 00:37:50

标签: arrays ruby hash

我有一系列哈希,信息如下。我正试图获得最大,最小和平均的时间睡眠"。我已经尝试过这些问题的答案,但它似乎没有用。

Find the largest value for an array of hashes with common keys?

Finding the element of a Ruby array with the maximum value for a particular attribute

rails select maximum value from array of hash

这是哈希数组

{"id"=>"1", "userId"=>"1", "day"=>"2015-12-05", "startTime"=>"2015-12-05T07:49:30.000Z", "endTime"=>"2015-12-05T15:56:30.000Z", "tzOffset"=>"-08:00", "source"=>"misfit", "mainSleep"=>true, "timeAsleep"=>478, "timeAwake"=>9, "efficiency"=>0, "timeToFallAsleep"=>0, "timeAfterWakeup"=>0, "numberOfWakeups"=>1, "timeInBed"=>0, "createdAt"=>"2015-12-05T21:59:24.935Z", "updatedAt"=>"2015-12-05T21:59:24.935Z", "humanId"=>"1"}
{"id"=>"2", "userId"=>"2", "day"=>"2015-12-04", "startTime"=>"2015-12-04T07:02:30.000Z", "endTime"=>"2015-12-04T14:59:30.000Z", "tzOffset"=>"-08:00", "source"=>"misfit", "mainSleep"=>true, "timeAsleep"=>471, "timeAwake"=>6, "efficiency"=>0, "timeToFallAsleep"=>0, "timeAfterWakeup"=>0, "numberOfWakeups"=>1, "timeInBed"=>0, "createdAt"=>"2015-12-04T16:41:44.198Z", "updatedAt"=>"2015-12-04T16:41:44.198Z", "humanId"=>"2"}
{"id"=>"3", "userId"=>"3", "day"=>"2015-12-03", "startTime"=>"2015-12-03T06:02:32.000Z", "endTime"=>"2015-12-03T13:38:32.000Z", "tzOffset"=>"-08:00", "source"=>"misfit", "mainSleep"=>true, "timeAsleep"=>434, "timeAwake"=>22, "efficiency"=>0, "timeToFallAsleep"=>0, "timeAfterWakeup"=>0, "numberOfWakeups"=>2, "timeInBed"=>0, "createdAt"=>"2015-12-03T15:35:42.903Z", "updatedAt"=>"2015-12-03T15:35:42.903Z", "humanId"=>"3"}
{"id"=>"4", "userId"=>"4", "day"=>"2015-12-02", "startTime"=>"2015-12-02T09:17:33.000Z", "endTime"=>"2015-12-02T15:27:33.000Z", "tzOffset"=>"-08:00", "source"=>"misfit", "mainSleep"=>true, "timeAsleep"=>370, "timeAwake"=>0, "efficiency"=>0, "timeToFallAsleep"=>0, "timeAfterWakeup"=>0, "numberOfWakeups"=>0, "timeInBed"=>0, "createdAt"=>"2015-12-03T04:00:02.050Z", "updatedAt"=>"2015-12-03T04:00:02.050Z", "humanId"=>"4"}
{"id"=>"5", "userId"=>"5", "day"=>"2015-12-01", "startTime"=>"2015-12-01T05:45:36.000Z", "endTime"=>"2015-12-01T13:50:36.000Z", "tzOffset"=>"-08:00", "source"=>"misfit", "mainSleep"=>true, "timeAsleep"=>485, "timeAwake"=>0, "efficiency"=>0, "timeToFallAsleep"=>0, "timeAfterWakeup"=>0, "numberOfWakeups"=>0, "timeInBed"=>0, "createdAt"=>"2015-12-01T17:50:20.168Z", "updatedAt"=>"2015-12-01T17:50:20.168Z", "humanId"=>"5"}
{"id"=>"6", "userId"=>"6", "day"=>"2015-11-30", "startTime"=>"2015-11-30T05:57:38.000Z", "endTime"=>"2015-11-30T14:23:38.000Z", "tzOffset"=>"-08:00", "source"=>"misfit", "mainSleep"=>true, "timeAsleep"=>499, "timeAwake"=>7, "efficiency"=>0, "timeToFallAsleep"=>0, "timeAfterWakeup"=>0, "numberOfWakeups"=>1, "timeInBed"=>0, "createdAt"=>"2015-11-30T16:18:38.276Z", "updatedAt"=>"2015-11-30T16:18:38.276Z", "humanId"=>"6"}

3 个答案:

答案 0 :(得分:3)

以下是如何做到这一点:

array.max_by {|x| x['timeAsleep']} # max
array.min_by {|x| x['timeAsleep']} # min
array.map {|x| x['timeAsleep']}.reduce(:+) / array.size.to_f # average

您可以在文档

上阅读有关mapreduce的信息

答案 1 :(得分:0)

arr = [
  {"id"=>"1", "timeAsleep"=>478, "timeAwake"=>9},
  {"id"=>"2", "timeAsleep"=>471, "timeAwake"=>6},
  {"id"=>"3", "timeAsleep"=>434, "timeAwake"=>22},
  {"id"=>"4", "timeAsleep"=>370, "timeAwake"=>0},
  {"id"=>"5", "timeAsleep"=>485, "timeAwake"=>502},
  {"id"=>"6", "timeAsleep"=>499, "timeAwake"=>7}] 

sleep_times = arr.map { |h| h["timeAsleep"] }
  #=> [478, 471, 434, 370, 485, 499]

timeAsleep

的最小值和最大值
sleep_times.minmax
  #=> [370, 499] 

timeAsleep

的平均值
sleep_times.reduce(:+).fdiv(arr.size).round(1)
  #=> 456.2

最大值所有具有数值的键

(我不确定你是否想要这个。)

arr.map { |h| h.values.select { |v| v.kind_of?(Numeric) }.max }.max
  #=> 502

答案 2 :(得分:0)

更有效的方法是对哈希数组进行一次排序,而不是查找最大/最小等。

array = array_hash.sort_by {|k| k["timeAsleep"] }

然后,您可以从已排序的数组

中检索相应的哈希值
puts "Hash with Smallest timeAsleep", array[0]
puts "Hash with Largest timeAsleep", array[array.length-1]

if ((array.length % 2) == 0)
    puts "Hash with Average timeAsleep", array[(array.size/2)-1]
else
    puts "Hash with Average timeAsleep", array[(array.size/2)]
end