在Ruby中需要将数组拆分为指定大小的子数组[复制]

时间:2022-05-07 21:43:53

This question already has an answer here:

这个问题已经有了答案:

I have an array that is something like this:

我有这样一个数组:

arr = [4, 5, 6, 7, 8, 4, 45, 11]

I want a fancy method like

我想要一个奇特的方法

sub_arrays = split (arr, 3)

This should return the following: [[4, 5, 6], [7,8,4], [45,11]]

这将返回以下内容:[[4,5,6],[7,8,4],[45,11]

4 个解决方案

#1


46  

arr.each_slice(3).to_a

each_slice returns an Enumerable, so if that's enough for you, you don't need to call to_a.

each_slice返回一个可枚举的值,所以如果这对您来说足够了,您不需要调用to_a。

In 1.8.6 you need to do:

在1.8.6中,您需要:

require 'enumerator'
arr.enum_for(:each_slice, 3).to_a

If you just need to iterate, you can simply do:

如果你只需要迭代,你可以简单地做:

arr.each_slice(3) do |x,y,z|
  puts(x+y+z)
end

#2


4  

can also utilize this with a specific purpose:

也可以利用这个特定的目的:

((1..10).group_by {|i| i%3}).values    #=> [[1, 4, 7, 10], [2, 5, 8], [3, 6, 9]]

in case you just want two partitioned arrays:

如果您只想要两个分区数组:

(1..6).partition {|v| v.even? }  #=> [[2, 4, 6], [1, 3, 5]]

#3


3  

If your using Rails 2.3+ you can do something like this:

如果您使用Rails 2.3+,您可以做以下事情:

arr.in_groups(3, false)

Checkout the api docs

结账的api文档

#4


1  

In Rails you can use method in_groups_of which splits an array into groups of specified size

在Rails中,可以使用方法in_groups_of将数组分割成指定大小的组

arr.in_groups_of(3) # => [[4, 5, 6], [7, 8, 4], [45, 11, nil]]
arr.in_groups_of(3, false) # => [[4, 5, 6], [7, 8, 4], [45, 11]]

Whereas method in_groups splits an array into specified number of balanced groups

而方法in_groups将数组分割为指定数量的均衡组

arr.in_groups(5) # => [[4, 5], [6, 7], [8, 4], [45, nil], [11, nil]]
arr.in_groups(5, false) # => [[4, 5], [6, 7], [8, 4], [45], [11]]

#1


46  

arr.each_slice(3).to_a

each_slice returns an Enumerable, so if that's enough for you, you don't need to call to_a.

each_slice返回一个可枚举的值,所以如果这对您来说足够了,您不需要调用to_a。

In 1.8.6 you need to do:

在1.8.6中,您需要:

require 'enumerator'
arr.enum_for(:each_slice, 3).to_a

If you just need to iterate, you can simply do:

如果你只需要迭代,你可以简单地做:

arr.each_slice(3) do |x,y,z|
  puts(x+y+z)
end

#2


4  

can also utilize this with a specific purpose:

也可以利用这个特定的目的:

((1..10).group_by {|i| i%3}).values    #=> [[1, 4, 7, 10], [2, 5, 8], [3, 6, 9]]

in case you just want two partitioned arrays:

如果您只想要两个分区数组:

(1..6).partition {|v| v.even? }  #=> [[2, 4, 6], [1, 3, 5]]

#3


3  

If your using Rails 2.3+ you can do something like this:

如果您使用Rails 2.3+,您可以做以下事情:

arr.in_groups(3, false)

Checkout the api docs

结账的api文档

#4


1  

In Rails you can use method in_groups_of which splits an array into groups of specified size

在Rails中,可以使用方法in_groups_of将数组分割成指定大小的组

arr.in_groups_of(3) # => [[4, 5, 6], [7, 8, 4], [45, 11, nil]]
arr.in_groups_of(3, false) # => [[4, 5, 6], [7, 8, 4], [45, 11]]

Whereas method in_groups splits an array into specified number of balanced groups

而方法in_groups将数组分割为指定数量的均衡组

arr.in_groups(5) # => [[4, 5], [6, 7], [8, 4], [45, nil], [11, nil]]
arr.in_groups(5, false) # => [[4, 5], [6, 7], [8, 4], [45], [11]]