【问题标题】:Ruby code to iterate through two array simulataneoulsyRuby代码遍历两个数组simulataneoulsy
【发布时间】:2013-10-04 11:19:25
【问题描述】:

如何同时在 ruby​​ 中迭代两个数组,我不想使用 for 循环。 例如,这是我的数组=

array 1=["a","b","c","d"]
array 2=[1,2,3,4]

【问题讨论】:

  • 无论如何你都不应该使用for。你几乎总是想要each
  • @theTinMan 除非你用有效的论据来支持你的主张(而且我已经提供了我的论据)——说我写的“毫无意义”只是侮辱(虽然我没有冒犯;) )。我知道 Ruby 程序员使用for 循环并不“常见”,但我看不出有什么好的理由。我完全同意这个人:paulphilippov.com/articles/enumerable-each-vs-for-loops-in-ruby
  • @alfasin 你用 Ruby 2.0 运行过这个基准测试吗?差异很小,但for 版本最慢。
  • for,当用于遍历数组时,还会公开应该是临时变量来保存元素的内容。这个新的临时变量使命名空间混乱,不像使用带有each 的块,它创建一个新的范围,隔离它的临时变量,当块退出时消失。此外,for 是使用 each 构建的,因此您会为语法糖增加一些额外的复杂性,从而减慢您的代码。暴露垃圾变量和减慢执行似乎是避免它的好理由。

标签: ruby


【解决方案1】:

您可以像这样使用 zip 功能:

array1.zip(array2).each do |array1_var, array2_var|
 ## whatever you want to do with array_1var and array_2 var 
 end

【讨论】:

  • 我认为这是因为提出问题的人不想使用 for 循环可能是因为某些使用原因,例如在某些情况下,我们有哈希数组,for 循环变得非常微不足道例如这样的数组:[{"name"=>"Default", "stocks"=>[{"name"=>"Ajanta Pharma Ltd.", "id"=>"id1", "qty"=>" 10"}]}, {"name"=>"low margin", "stocks"=>[{"name"=>"Atul Auto Ltd.", "id"=>"id5", "qty"=> "12"}, {"name"=>"new", "stocks"=>[{"name"=>"Hindustan Zinc Ltd.", "id"=>"id6", "qty"=>"13 "}]}]
  • 他们是否也可以为此添加索引?为了跟踪循环计数。
【解决方案2】:

你可以使用Array#zip(不需要使用each,因为zip接受可选块):

array1 = ["a","b","c","d"]
array2 = [1,2,3,4]

array1.zip(array2) do |a, b|
  p [a,b]
end

或者,Array#transpose

[array1, array2].transpose.each do |a, b|
  p [a,b]
end

【讨论】:

    【解决方案3】:

    您可以将它们zip 放在一起,然后使用each 遍历这些对。

    array1.zip(array2).each do |pair|
      p pair
    end
    

    【讨论】:

      【解决方案4】:

      如果两个数组大小相同,您可以这样做:

      array1=["a","b","c","d"]
      array2=[1,2,3,4]
      
      for i in 0..arr1.length do
       //here you do what you want with array1[i] and array2[i]
      end
      

      【讨论】:

        【解决方案5】:

        假设两个数组大小相同,您可以使用each_with_index 遍历它们,使用第二个数组的索引:

        array1.each_with_index do |item1, index|
          item2 = array2[index]
          # do something with item1, item2
        end
        

        像这样:

        irb(main):007:0> array1.each_with_index do |item1, index|
        irb(main):008:1*   item2 = array2[index]
        irb(main):009:1>   puts item1, item2
        irb(main):010:1> end
        a
        1
        b
        2
        c
        3
        d
        4
        

        【讨论】:

          【解决方案6】:

          当两个数组的大小相同时,您可以执行以下操作:

          array1=["a","b","c","d"]
          array2=[1,2,3,4]
          
          array2.each_index{|i| p "#{array2[i]},#{array1[i]} at location #{i}"}
          
          # >> "1,a at location 0"
          # >> "2,b at location 1"
          # >> "3,c at location 2"
          # >> "4,d at location 3"
          

          如果有可能一个数组大于另一个数组,则必须调用larger_array#each_index

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-09-07
            • 2011-04-04
            • 2020-05-03
            • 2015-09-26
            相关资源
            最近更新 更多