Itertools in Julia

52 views Asked by At

I have array N-M dimension array -A. I need to generate the combinations of one element from each string and determine their sum.

Example, A=[[1,2],[3,4],[5,6]]. k=[1,3,5],[1,4,5],[1,4,6],[2,3,5],[2,3,6],[2,4,5],[2,4,6].

Thus k has three indexes for each combination. The summ=9, 10, 11, 10, 11, 11, 12. Using python, I can do it with

sums = ((vs,sum(vs)) for vs in itertools.product(*A))
for k,v in sums:
    print v
    print k

How to do it using Julia?

1

There are 1 answers

0
Dan Getz On
julia> A=[[1,2],[3,4],[5,6]]; vec([(sum(v), v) for v in Iterators.product(A...)])
8-element Vector{Tuple{Int64, Tuple{Int64, Int64, Int64}}}:
 (9, (1, 3, 5))
 (10, (2, 3, 5))
 (10, (1, 4, 5))
 (11, (2, 4, 5))
 (10, (1, 3, 6))
 (11, (2, 3, 6))
 (11, (1, 4, 6))
 (12, (2, 4, 6))

seems pretty close.