【问题标题】:Split an array into new arrays (each with a unique name) [duplicate]将数组拆分为新数组(每个数组都有一个唯一的名称)[重复]
【发布时间】:2015-07-11 17:36:14
【问题描述】:

我正在尝试将数组切成相等的大小(向下舍入)并将每个部分保存到相应的变量中。

each_slice 方法已用于抓取 n 大小的块。但是我想不出办法:

  1. 遍历每个块的“子索引”
  2. 为每个数组创建一个新数组并给每个数组一个唯一的名称。

    letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n"]
    
       def groups_of_five(array)
       split_array = array.each_slice(5).to_a
    
       #something like the following:
       #array(n) = Array[split_array.each {|x| x}]
    
       end 
    end
    

我希望的输出:

groups_of_five(letters)
=> array1: ["a,"b","c","d","e"]
=> array2: ["f","g","h","i","j"]
=> array3: ["k","l","m","n"]

【问题讨论】:

  • 你想要来自 groups_of_five(letters) 的哈希结构,比如{array1: ["a,"b","c","d","e"],array2: ["f","g","h","i","j"]} 对吧?

标签: ruby arrays iteration slice


【解决方案1】:

你可以这样做:

def group_em(a,n)
  arr = a.dup
  (1..(arr.size.to_f/n).ceil).each_with_object({}) { |i,h|
    h["array#{i}"] = arr.shift(n) }
end

group_em(letters,1)
  #=> {"array1"=>["a"], "array2"=>["b"],...,"array14"=>["n"]} 
group_em(letters,2)
  #=> {"array1"=>["a", "b"], "array2"=>["c", "d"],...,"array7"=>["m", "n"]} 
group_em(letters,5)
  #=> {"array1"=>["a", "b", "c", "d", "e"],
  #    "array2"=>["f", "g", "h", "i", "j"],
  #    "array3"=>["k", "l", "m", "n"]} 

变体是:

def group_em(arr,n)
  (1..(arr.size.to_f/n).ceil).zip(arr.each_slice(n).to_a)
    .each_with_object({}) { |(i,a),h| h["array#{i}"]=>a) }
end

【讨论】:

    【解决方案2】:

    each_slicewith_index 结合使用,您将拥有所需的一切:

    letters.each_slice(5).with_index(1) do |group, index| 
      puts "array#{index}: #{group.inspect}"
    end
    

    输出是:

    array1: ["a", "b", "c", "d", "e"]
    array2: ["f", "g", "h", "i", "j"]
    array3: ["k", "l", "m", "n"]
    

    在高于 1.8 的 Ruby 版本中不再可以动态设置局部变量,所以如果你想分配给变量,它必须是实例变量,或者你可以输出一个 Hash。

    以下将创建实例变量:

    def groups_of_five(array)
      array.each_slice(5).with_index(1) do |group, index| 
        instance_variable_set "@array#{index}".to_sym, group
      end
    end
    
    groups_of_five(letters)
    puts @array1 #=> ["a", "b", "c", "d", "e"]
    puts @array2 #=> ["f", "g", "h", "i", "j"]
    puts @array3 #=> ["k", "l", "m", "n"]
    

    或者这会输出一个哈希:

    def groups_of_five(array)
      hash = {}
      array.each_slice(5).with_index(1) do |group, index| 
        hash["array#{index}".to_sym] = group
      end
      hash
    end
    
    hash = groups_of_five(letters)
    puts hash[:array1] #=> ["a", "b", "c", "d", "e"]
    puts hash[:array2] #=> ["f", "g", "h", "i", "j"]
    puts hash[:array3] #=> ["k", "l", "m", "n"]
    

    【讨论】:

    • 太棒了,谢谢。现在要了解发生了什么!我喜欢使用“组”和“索引”。早些时候我称它为“子组”,但似乎是一个更清晰的约定。我想我不清楚这是如何创建新数组的。我看到它 新行。但这些是实际的数组吗?
    • 更新了几个新示例。我个人会选择最后一个哈希输出选项。
    • 您可以写with_index(1) 让索引从1 开始。 (这不是each_with_index 的选项)。
    • 谢谢@CarySwoveland,我不知道那个。我会更新我的答案。
    • 感谢@CarySwoveland。我仍然有点好奇这是如何创建新数组的。 语句中是否有一些我没有看到的诡计?感谢您帮助初学者。
    【解决方案3】:

    如果您正在寻找从 groups_of_five(letters) 返回的哈希结构,这就是解决方案

    def groups_of_five(array)
       split_array = letters.each_slice(5).to_a
       split_array.reduce({}){ |i,a|
           index = split_array.index(a) + 1
           i["array#{index}"] = a; i
       }
    end
    
    # groups_of_five(letters)
    #=> {"array1"=>["a", "b", "c", "d", "e"], "array2"=>["f", "g", "h", "i", "j"], "array3"=>["k", "l", "m", "n"]}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多