I'm looking to find all combinations of single items from a variable number of arrays. How do I do this in Ruby?
Given two arrays, I can use Array.product like this:
开发者_运维知识库groups = []
groups[0] = ["hello", "goodbye"]
groups[1] = ["world", "everyone"]
combinations = groups[0].product(groups[1])
puts combinations.inspect 
# [["hello", "world"], ["hello", "everyone"], ["goodbye", "world"], ["goodbye", "everyone"]]
How could this code work when groups contains a variable number of arrays?
groups = [
  %w[hello goodbye],
  %w[world everyone],
  %w[here there]
]
combinations = groups.first.product(*groups.drop(1))
p combinations
# [
#   ["hello", "world", "here"],
#   ["hello", "world", "there"],
#   ["hello", "everyone", "here"],
#   ["hello", "everyone", "there"],
#   ["goodbye", "world", "here"],
#   ["goodbye", "world", "there"],
#   ["goodbye", "everyone", "here"],
#   ["goodbye", "everyone", "there"]
# ]
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论