在 ruby 中将数组拆分为相等的部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12374645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Splitting an array into equal parts in ruby
提问by Red
I need a way to split an array in to a bunch of arrays within another array of equal size. Anyone have any method of doing this?
我需要一种方法将一个数组拆分为另一个大小相同的数组中的一堆数组。任何人都有这样做的方法吗?
For instance
例如
a = [0, 1, 2, 3, 4, 5, 6, 7]
a.method_i_need(3)
a.inspect
=> [[0,1,2], [3,4,5], [6,7]]
回答by Joshua Cheek
You're looking for Enumerable#each_slice
您正在寻找 Enumerable#each_slice
a = [0, 1, 2, 3, 4, 5, 6, 7]
a.each_slice(3) # => #<Enumerator: [0, 1, 2, 3, 4, 5, 6, 7]:each_slice(3)>
a.each_slice(3).to_a # => [[0, 1, 2], [3, 4, 5], [6, 7]]
回答by mltsy
Perhaps I'm misreading the question since the other answer is already accepted, but it sounded like you wanted to split the array in to 3 equal groups, regardless of the size of each group, rather than split it into N groups of 3 as the previous answers do. If that's what you're looking for, Rails (ActiveSupport) also has a method called in_groups:
也许我误读了这个问题,因为另一个答案已经被接受,但听起来你想将数组分成 3 个相等的组,而不管每组的大小,而不是将它分成 N 组,每组 3 作为以前的答案。如果这就是您要查找的内容,Rails (ActiveSupport) 也有一个名为in_groups的方法:
a = [0,1,2,3,4,5,6]
a.in_groups(2) # => [[0,1,2,3],[4,5,6,nil]]
a.in_groups(3, false) # => [[0,1,2],[3,4], [5,6]]
I don't think there is a ruby equivalent, however, you can get roughly the same results by adding this simple method:
我不认为有 ruby 等价物,但是,通过添加这个简单的方法,您可以获得大致相同的结果:
class Array; def in_groups(num_groups)
return [] if num_groups == 0
slice_size = (self.size/Float(num_groups)).ceil
groups = self.each_slice(slice_size).to_a
end; end
a.in_groups(3) # => [[0,1,2], [3,4,5], [6]]
The only difference (as you can see) is that this won't spread the "empty space" across all the groups; every group but the last is equal in size, and the last group always holds the remainder plus all the "empty space".
唯一的区别(如您所见)是这不会在所有组中传播“空白空间”;除最后一组外的每个组的大小都相等,最后一组总是包含余数加上所有“空白空间”。
Update:As @rimsky astutely pointed out, the above method will not always result in the correct number of groups (sometimes it will create multiple "empty groups" at the end, and leave them out). Here's an updated version, pared down from ActiveSupport's definitionwhich spreads the extras out to fill the requested number of groups.
更新:正如@rimsky 敏锐地指出的那样,上述方法并不总是会产生正确数量的组(有时它会在最后创建多个“空组”,并将它们排除在外)。这是一个更新的版本,从ActiveSupport 的定义中删减,它扩展了额外的内容以填充请求的组数。
def in_groups(number)
group_size = size / number
leftovers = size % number
groups = []
start = 0
number.times do |index|
length = group_size + (leftovers > 0 && leftovers > index ? 1 : 0)
groups << slice(start, length)
start += length
end
groups
end
回答by Paritosh Singh
Try
尝试
a.in_groups_of(3,false)
It will do your job
它会做你的工作
回答by Maxime Brehin
As mltsy wrote, in_groups(n, false)should do the job.
正如 mltsy 所写,in_groups(n, false)应该完成这项工作。
I just wanted to add a small trick to get the right balance
my_array.in_group(my_array.size.quo(max_size).ceil, false).
我只是想添加一个小技巧来获得正确的平衡
my_array.in_group(my_array.size.quo(max_size).ceil, false)。
Here is an example to illustrate that trick:
下面是一个例子来说明这个技巧:
a = (0..8).to_a
a.in_groups(4, false) => [[0, 1, 2], [3, 4], [5, 6], [7, 8]]
a.in_groups(a.size.quo(4).ceil, false) => [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
回答by android.weasel
This needs some better cleverness to smear out the extra pieces, but it's a reasonable start.
这需要一些更好的聪明来抹掉多余的部分,但这是一个合理的开始。
def i_need(bits, r)
c = r.count
(1..bits - 1).map { |i| r.shift((c + i) * 1.0 / bits ) } + [r]
end
> i_need(2, [1, 3, 5, 7, 2, 4, 6, 8])
=> [[1, 3, 5, 7], [2, 4, 6, 8]]
> i_need(3, [1, 3, 5, 7, 2, 4, 6, 8])
=> [[1, 3, 5], [7, 2, 4], [6, 8]]
> i_need(5, [1, 3, 5, 7, 2, 4, 6, 8])
=> [[1, 3], [5, 7], [2, 4], [6], [8]]

