简体   繁体   English

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

[英]Find the largest, smallest, and average value for an array of hashes with common keys?

I have an array of hashes with the information below. 我有一系列带有以下信息的哈希。 I am trying to get the largest, smallest, and average for "timeAsleep". 我正在尝试获取“ timeAsleep”的最大,最小和平均值。 I have tried the answers from these questions, but it doesn't seem to work. 我已经尝试过从这些问题中获得答案,但是似乎没有用。

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 查找具有特定属性最大值的Ruby数组的元素

rails select maximum value from array of hash rails从哈希数组中选择最大值

Here is the array of hashes 这是哈希数组

{"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"}

Here is how you can do it: 这是您可以执行的操作:

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

You can read about map and reduce on the documentation 您可以阅读有关mapreduce文档数量

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]

Min and max value for timeAsleep 最小和最大值timeAsleep

sleep_times.minmax
  #=> [370, 499] 

Mean for timeAsleep 平均timeAsleep

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

Max value all keys with numeric values 最大值所有具有数字值的键

(I'm not sure if you wanted this.) (我不确定您是否想要这个。)

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

A more efficient way will be to sort the array of hashes once instead of finding max/min etc. 一种更有效的方法是将哈希数组排序一次,而不是查找max / min等。

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

Then you can just retrieve the respective hashes from the sorted array 然后,您可以从排序后的数组中检索各个哈希

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM