如何在 Julia 中对可变数量的列表进行笛卡尔积?

gra*_*123 2 arrays cartesian-product julia

对于集合 {1, 2, ..., n} 中的每个值 j,其中 n 的值可以变化(它是我的程序中的某个变量,可以根据用户的输入而不同),我有一个数组A_j。我想获得所有数组 A_j 的笛卡尔积,以便我可以迭代该笛卡尔积(从每个 A_1、A_2、... A_n 中取出一个元素以获得一个元组 (a_1、a_2、... , a_n) 在 A_1 x A_2 x ... x A_n) 中。我将如何在 Julia 中实现这一目标?

Vin*_* Yu 5

使用Iterators.product

\n
help?> Iterators.product\n  product(iters...)\n\n  Return an iterator over the product of several iterators. Each generated\n  element is a tuple whose ith element comes from the ith argument iterator.\n  The first iterator changes the fastest.\n\n  Examples\n  \xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\n\n  julia> collect(Iterators.product(1:2, 3:5))\n  2\xc3\x973 Matrix{Tuple{Int64, Int64}}:\n   (1, 3)  (1, 4)  (1, 5)\n   (2, 3)  (2, 4)  (2, 5)\n
Run Code Online (Sandbox Code Playgroud)\n