I would like to create buckets on List<double>
such as divided in n
groups such as:
List<double> list = new List<double>() {
0, 0.1, 1.1, 2.2, 3.3, 4.1, 5.6, 6.3, 7.1, 8.9, 9.8, 9.9, 10
};
n = 5
I want to obtain something like this
bucket values
---------------------------------
[0 .. 2] -> {0, 0.1, 1.1}
[2 .. 4] -> {2.2, 3.3}
...
[8 .. 10] -> {8.9, 9.8, 9.9, 10}
The problem is if I do a GroupBy
using:
return items
.Select((item, inx) => new { item, inx })
.GroupBy(x => Math.Floor(x.item / step))
.Select(g => g.Select(x => x.item));
I always get unwanted first or last bucket such as [10 .. 12]
(note that all the values are in [0 .. 10]
range) or [0 .. 0]
(note the wrong range of the bucket) which contains extreme values only (0
or 10
in the example above).
any Help ?