数组数组上的迭代器积

tuc*_*son 8 julia

如何从数组数组创建数组乘积的迭代器?数组大小未预先确定。

基本上以下工作如我所愿:

for i in Base.Iterators.product([1,2,3],[4,5])
   print(i)
end
(1, 4)(2, 4)(3, 4)(1, 5)(2, 5)(3, 5)
Run Code Online (Sandbox Code Playgroud)

但我希望它适用于一组数组,但我得到了不同的结果:

x = [[1,2,3],[4,5]]
for i in Base.Iterators.product(x)
   print(i)
end
([1, 2, 3],)([4, 5],)
Run Code Online (Sandbox Code Playgroud)

Dav*_*ela 5

您可以使用splat 运算符将数组数组插入到函数调用中:

julia> x = [[1,2,3],[4,5]];

julia> for i in Base.Iterators.product(x...)
          print(i)
       end
(1, 4)(2, 4)(3, 4)(1, 5)(2, 5)(3, 5)
Run Code Online (Sandbox Code Playgroud)