Ruby 阵列半平坦
要将其转换为:Ruby 阵列半平坦,ruby,arrays,arguments,apply,Ruby,Arrays,Arguments,Apply,要将其转换为: [["1", "2", "3"], ["4", "5", "6"]] 为此: ["1", "2", "3"], ["4", "5", "6"] 要传递到Array.product(),第一个数组可以包含未知数量的其他数组。例如,给定的数组也可以是 [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]] 最后,我需要通过以下论证: otherArray.product(["1", "2", "3"], ["4", "5", "6
[["1", "2", "3"], ["4", "5", "6"]]
为此:
["1", "2", "3"], ["4", "5", "6"]
要传递到Array.product(),第一个数组可以包含未知数量的其他数组。例如,给定的数组也可以是
[["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]]
最后,我需要通过以下论证:
otherArray.product(["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"])
提前谢谢 撇开最后一行代码不谈,其余部分似乎可以通过使用0索引来解决:
arr[0]
*在参数列表中用于将数组内容解压缩为参数(如
此处)或将参数打包到数组中,如“def mymethod(*args)”
参考资料:我认为使用Ruby的阵列扩展对您有效:
a=[[1,2,3],[4,5,6]]
b=[1,2,3].product([1,2,3],[4,5,6])
c=[1,2,3].product(*a)
b == c #This should be true
基本上,将星号(*)放在变量前面会将数组中的所有元素展开为一个参数列表,这就是您想要的。我不这么认为,这只会从其他数组返回第一个数组,不会动态包含所有数组。在Python中,这也是一个非常有用的运算符。
a=[[1,2,3],[4,5,6]]
b=[1,2,3].product([1,2,3],[4,5,6])
c=[1,2,3].product(*a)
b == c #This should be true