【问题标题】:How do I get the results of a split returned in the order in which they occur in the split?如何按照拆分中出现的顺序返回拆分结果?
【发布时间】:2017-08-03 22:48:42
【问题描述】:

我使用的是 Ruby 2.4。我正在尝试拆分数组中的元素。我想要的是用我的拆分结果形成等效的数组。我希望拆分的第一部分是第一个数组,而拆分的第二部分是第二个数组。所以我有

data_col = ["mm a", "nn b", "nn a"]
arr1, arr2 = data_col.map do |x| 
  if x
    a, b, c = x.partition(/(^|[[:space:]]+)[ab]$/i)
    [b.strip, a + c] 
  else
      [nil, nil]
  end
end.transpose
 #=> [["a", "b", "a"], ["mm", "nn", "nn"]]

问题是,拆分工作正常,但数组正在反转。我希望 ["mm", "nn", "nn"] 成为数组中的第一个元素。我该如何重写才能正确返回数组——也就是说,拆分的第一部分在第一个数组中,而拆分的第二部分在第二个数组中?

【问题讨论】:

    标签: arrays ruby split ruby-2.4


    【解决方案1】:
    data_col = ["mm a", "nn b", "nn a"]
    arr1, arr2 = data_col.map do |x| 
      a, b, c = x.partition(/(^|[[:space:]]+)[ab]$/i)
      [a + c, b.strip] # <=================== switched b.strip and a+b
    end.transpose
    #=> [["mm", "nn", "nn"], ["a", "b", "a"]]
    

    我删除了if x 条件,因为在您的mapping 中无法从if x 获取false :)

    【讨论】:

      【解决方案2】:

      我认为你甚至不需要使用带有正则表达式的分区来获取 o/p,你可以简单地使用带有 split 和 transpose 的 map 来获得相同的 o/p,而且与使用正则表达式的分区相比它非常快.以下是 5 秒的简单基准测试,

      require 'benchmark/ips'
      
      data_col = ["mm a", "nn b", "nn a"]
      
      Benchmark.ips do |x|
        x.config(time: 5, warmup: 2)
        x.report('REGEXP') do
          arr1, arr2 = data_col.map do |xx|
            a, b, c = xx.partition(/(^|[[:space:]]+)[ab]$/i)
            [a + c, b.strip]
          end.transpose 
        end
      
        x.report('MAP SPLIT') do
          arr1, arr2 = data_col.map do |xx|
            xx.split(' ').map(&:strip)
          end.transpose
        end
        x.compare!
      end
      

      下面是IPS对比,

      Warming up --------------------------------------
                    REGEXP    16.985k i/100ms
                 MAP SPLIT    26.771k i/100ms
      Calculating -------------------------------------
                    REGEXP    190.220k (± 4.5%) i/s -    951.160k in   5.012963s
                 MAP SPLIT    303.243k (± 3.5%) i/s -      1.526M in   5.040226s
      
      Comparison:
                 MAP SPLIT:   303243.1 i/s
                    REGEXP:   190219.6 i/s - 1.59x  slower
      

      您可以看到带有 split 的 map 正在执行 303243.1 i/s,而带有 regex 的分区正在执行 190219.6 i/s。因此,使用 split 的 map 比使用 regex 的 partition 快 1.59 倍

      【讨论】:

        猜你喜欢
        • 2019-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-28
        • 1970-01-01
        • 2012-06-22
        相关资源
        最近更新 更多