简体   繁体   English

数组数组,到单个数组Ruby

[英]Array of Arrays, to individual Arrays Ruby

I have an array like the following: 我有一个像下面这样的数组:

[["red", "green", "blue"], ["small", "large", "medium"], ["loose", "tight"]]

I need to pull all of these arrays out of the parent, into separate arrays, like this: 我需要将所有这些数组从父级中拉出到单独的数组中,如下所示:

["red", "green", "blue"] ["small", "large", "medium"] ["loose", "tight"]

The program won't always know how many child arrays will be in the parent array, because they are dynamically created. 该程序并不总是知道父数组中将有多少个子数组,因为它们是动态创建的。

The end goal is to take the first array (red, green, blue, etc...) and call .product on it, passing in all the following arrays, individually, so I end up with the following: 最终目标是采用第一个数组(红色,绿色,蓝色等)并在其上调用.product ,并分别传递以下所有数组,因此我得到以下结果:

"red small loose", "red small right", "red large loose", etc..

This would work: 这将工作:

arrays = [["red", "green", "blue"], ["small", "large", "medium"], ["loose", "tight"]]

first, *others = arrays
first.product(*others).map { |s| s.join(' ') }
#=> ["red small loose", "red small tight", "red large loose", "red large tight", "red medium loose", "red medium tight", "green small loose", "green small tight", "green large loose", "green large tight", "green medium loose", "green medium tight", "blue small loose", "blue small tight", "blue large loose", "blue large tight", "blue medium loose", "blue medium tight"]

Or without temporary variables: 或没有临时变量:

arrays[0].product(*arrays[1..-1]).map { |s| s.join(' ') }

Note that the argument for product is prefixed with a * . 请注意, product的参数以*开头。 This is called the splat operator - it turns an array into an argument list. 这称为splat运算符 -将数组转换为参数列表。

I think you can do this: 我认为您可以这样做:

2.1.0 :001 > parent_array = [["red", "green", "blue"], ["small", "large", "medium"], ["loose", "tight"]]
 => [["red", "green", "blue"], ["small", "large", "medium"], ["loose", "tight"]] 
2.1.0 :002 > first_array = parent_array.shift
 => ["red", "green", "blue"] 
2.1.0 :003 > first_array
 => ["red", "green", "blue"] 
2.1.0 :004 > parent_array
 => [["small", "large", "medium"], ["loose", "tight"]] 

# Use the splat operator (*)
2.1.0 :006 > product = first_array.product(*parent_array)
 => [["red", "small", "loose"], ["red", "small", "tight"], ["red", "large", "loose"], ["red", "large", "tight"], ["red", "medium", "loose"], ["red", "medium", "tight"], ["green", "small", "loose"], ["green", "small", "tight"], ["green", "large", "loose"], ["green", "large", "tight"], ["green", "medium", "loose"], ["green", "medium", "tight"], ["blue", "small", "loose"], ["blue", "small", "tight"], ["blue", "large", "loose"], ["blue", "large", "tight"], ["blue", "medium", "loose"], ["blue", "medium", "tight"]] 
2.1.0 :007 > result = product.map {|array| array.join(' ') }
 => ["red small loose", "red small tight", "red large loose", "red large tight", "red medium loose", "red medium tight", "green small loose", "green small tight", "green large loose", "green large tight", "green medium loose", "green medium tight", "blue small loose", "blue small tight", "blue large loose", "blue large tight", "blue medium loose", "blue medium tight"] 

This is one of those problems for which there is really only one good solution, here the one given by @Stefan. 这是实际上只有一个好的解决方案的问题之一,这里是@Stefan给出的解决方案。 Still, it is fun, and possibly instructive, to try to dream up other ways. 尽管如此,尝试以其他方式做梦还是很有趣的,并且可能是有益的。 Here's a recursive approach: 这是一种递归方法:

arr = [["red",   "green", "blue"],
       ["small", "large", "medium"],
       ["loose", "tight"]]

def combo(arr)
  return arr.first.product(arr.last) if arr.size == 2
  combo(arr[1..-1]).flat_map { |c| arr.first.map { |f| [f, *c] } }
end

combo(arr)
  #=> [["red",   "small",  "loose"], ["green", "small",  "loose"],
  #    ["blue",  "small",  "loose"], ["red",   "small",  "tight"],
  #    ["green", "small",  "tight"], ["blue",  "small",  "tight"],
  #    ["red",   "large",  "loose"], ["green", "large",  "loose"],
  #    ...
  #    ["green", "medium", "tight"], ["blue",  "medium", "tight"]]

I have assumed that the order of the elements in the array returned is not important. 我假设返回数组中元素的顺序并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM