Ruby Array to Histogram:如何按范围对数字进行分组?

时间:2022-02-15 14:58:21

I'm trying to group an array of integers into an hash based on where the individual values fall in a range. Basically I want to convert an array to a fixed-width histogram.

我正在尝试根据各个值落在范围内的位置将整数数组分组到散列中。基本上我想将数组转换为固定宽度的直方图。

Example:

例:

values = [1,3,4,4,4,4,4,10,12,15,18]
bin_width = 3

I need to group the array values into a range-based historgram by where they fall into a 3-unit wide bucket like so:

我需要将数组值分组到基于范围的历史记录中,它们落入一个3单位宽的桶中,如下所示:

{'0..2'=>[1,3],'3..5'=>[4,4,4,4,4],'6..8'=>[],'9..11'=>[10]....

Is there a simple one line solution ( maybe something like values.group_by{|x| #range calc}) that would work here?

有一个简单的单行解决方案(可能像values.group_by {| x | #range calc})可以在这里工作吗?

2 个解决方案

#1


10  

values = [1, 7, 2, 8, 2]
values.group_by { |x| x / 3 }.map { |k, vs| [(3*k..3*k+2), vs] }.to_h
#=> {0..2=>[1, 2, 2], 6..8=>[7, 8]}

If you really need the empty ranges, I don't think a clean one-liner is possible. But this should do:

如果你真的需要空的范围,我认为不可能有干净的单线。但这应该做到:

grouped = values.group_by { |x| x / 3 }
min, max = grouped.keys.minmax
(min..max).map { |n| [(3*n..3*n+2), grouped.fetch(n, [])] }.to_h
#=> {0..2=>[1, 2, 2], 3..5=>[], 6..8=>[7, 8]}

#2


4  

I came up with a rather inefficient but quite clear solution:

我提出了一个相当低效但非常明确的解决方案:

ranges = 0.step(values.max, bin_width).each_cons(2).map { |s, e| Range.new(s, e, true) }
values.group_by { |v| ranges.find { |r| r.cover? v } }

#1


10  

values = [1, 7, 2, 8, 2]
values.group_by { |x| x / 3 }.map { |k, vs| [(3*k..3*k+2), vs] }.to_h
#=> {0..2=>[1, 2, 2], 6..8=>[7, 8]}

If you really need the empty ranges, I don't think a clean one-liner is possible. But this should do:

如果你真的需要空的范围,我认为不可能有干净的单线。但这应该做到:

grouped = values.group_by { |x| x / 3 }
min, max = grouped.keys.minmax
(min..max).map { |n| [(3*n..3*n+2), grouped.fetch(n, [])] }.to_h
#=> {0..2=>[1, 2, 2], 3..5=>[], 6..8=>[7, 8]}

#2


4  

I came up with a rather inefficient but quite clear solution:

我提出了一个相当低效但非常明确的解决方案:

ranges = 0.step(values.max, bin_width).each_cons(2).map { |s, e| Range.new(s, e, true) }
values.group_by { |v| ranges.find { |r| r.cover? v } }