在ruby中将数组拆分为多个小数组的最佳方法

在ruby中将数组拆分为多个小数组的最佳方法,ruby,arrays,split,Ruby,Arrays,Split,根据某些条件将阵列拆分为多个阵列的最简单方法是什么?在我的场景中,我需要将整数和字符串值移动到不同的数组中。我尝试了split方法,但没有按预期工作 x=[1,2,3,"a","b",4] x.split {|item| item.kind_of? Fixnum} 在C#中,在Linq中有一个groupby选项,可帮助您根据条件对对象进行分组。在对象上是否有类似的方法(不使用activerecord) 有简单的方法吗?以下是我的解决方案: hash = x.group_by { |t| t.k

根据某些条件将阵列拆分为多个阵列的最简单方法是什么?在我的场景中,我需要将整数和字符串值移动到不同的数组中。我尝试了
split
方法,但没有按预期工作

x=[1,2,3,"a","b",4]
x.split {|item| item.kind_of? Fixnum}
在C#中,在
Linq
中有一个
groupby
选项,可帮助您根据条件对对象进行分组。在
对象上是否有类似的方法(不使用activerecord)

有简单的方法吗?

以下是我的解决方案:

hash = x.group_by { |t| t.kind_of? Fixnum }
# hash  => {true=>[1, 2, 3, 4], false=>["a", "b"]} 
array1 = hash[true] # The array of integers
array2 = hash[false] # The array of strings
您正在寻找:

尝试:

您可以通过调用
返回一个数组,在您给出的示例中,该结果将返回:

[[Fixnum, [1, 2, 3, 4]], [String, ["a", "b"]]]

只需向池中加入更多解决方案:

x = [1,2,3,"a","b",4]

numbers = x.select{ |e| e.is_a?(Fixnum) } # => [1, 2, 3, 4]
letters = x - numbers # => ["a", "b"]

numbers = x.select{ |e| e.kind_of?(Fixnum) } # => [1, 2, 3, 4]
letters = x - numbers # => ["a", "b"]

以及一些显示细微变化如何影响速度的基准:

require 'benchmark'

x = [1,2,3,"a","b",4] * 100
n = 10_000
Benchmark.bm do |bench|
  bench.report { n.times {
    numbers = x.select{ |e| e.is_a?(Fixnum) }
    letters = x - numbers
  }}

  bench.report { n.times {
    numbers = x.select{ |e| e.kind_of?(Fixnum) }
    letters = x - numbers
  }}

  bench.report { n.times {
    (numbers, letters) = x.group_by {|a| a.class}.values_at(Fixnum, String)
  }}

  bench.report { n.times {
    numbers, not_numbers = x.partition{|item| item.kind_of? Fixnum}
  }}
end
# >>       user     system      total        real
# >>   4.270000   0.010000   4.280000 (  4.282922)
# >>   4.290000   0.000000   4.290000 (  4.288720)
# >>   5.160000   0.010000   5.170000 (  5.163695)
# >>   3.720000   0.000000   3.720000 (  3.721459)

+1这也是速度的一个坚实的胜利!请参阅我添加的基准测试。下面的答案使用
groupby
,这允许拆分为两个以上的组。奇怪的是,我发现
.partition
甚至比创建两个数组并执行
更快。每个
如果e.kind\u of(Fixnum)
。我猜他们正在为C语言中的
.partition
做一些优化。我没有看过源代码,但我有同样的怀疑。
x = [1,2,3,"a","b",4]

numbers = x.select{ |e| e.is_a?(Fixnum) } # => [1, 2, 3, 4]
letters = x - numbers # => ["a", "b"]

numbers = x.select{ |e| e.kind_of?(Fixnum) } # => [1, 2, 3, 4]
letters = x - numbers # => ["a", "b"]
(numbers, letters) = x.group_by {|a| a.class}.values_at(Fixnum, String)
numbers # => [1, 2, 3, 4]
letters # => ["a", "b"]
require 'benchmark'

x = [1,2,3,"a","b",4] * 100
n = 10_000
Benchmark.bm do |bench|
  bench.report { n.times {
    numbers = x.select{ |e| e.is_a?(Fixnum) }
    letters = x - numbers
  }}

  bench.report { n.times {
    numbers = x.select{ |e| e.kind_of?(Fixnum) }
    letters = x - numbers
  }}

  bench.report { n.times {
    (numbers, letters) = x.group_by {|a| a.class}.values_at(Fixnum, String)
  }}

  bench.report { n.times {
    numbers, not_numbers = x.partition{|item| item.kind_of? Fixnum}
  }}
end
# >>       user     system      total        real
# >>   4.270000   0.010000   4.280000 (  4.282922)
# >>   4.290000   0.000000   4.290000 (  4.288720)
# >>   5.160000   0.010000   5.170000 (  5.163695)
# >>   3.720000   0.000000   3.720000 (  3.721459)