我可以像这样完成一个两级嵌套循环
for i1 in 1:n
for i2 in 1:n
do something with (i1,i2)
我要如何将这个扩展到任意嵌套层数的循环中呢?
举例来说,在Python中,我可以使用以下代码循环n^m的笛卡尔积:
for i in (itertools.product(xrange(n),repeat=m)):
喜欢
for i in (itertools.product(xrange(2),repeat=3)):
print i
(0, 0, 0)
(0, 0, 1)
(0, 1, 0)
(0, 1, 1)
(1, 0, 0)
(1, 0, 1)
(1, 1, 0)
(1, 1, 1)
感谢@tholy的评论。我已成功应用Iterators.jl。由于我是Julia的新手,所以我的代码可能有些拙劣。
for i in product(repmat(Any[1:2],3)...)
println(i)
end
(1,1,1)
(2,1,1)
(1,2,1)
(2,2,1)
(1,1,2)
(2,1,2)
(1,2,2)
(2,2,2)
test.jl
脚本应该能帮助你入门。 - tholy