algorithm - python combinations of multiple list -
is there pythonic method generate combinations between multiple list? (similar cartesian product more complicated)
example:
a = [1, 2, 3] b = [4, 5, 6] c = [7, 8, 9] # ... # there more 3 lists
expected output:
1. [(1, 4, 7), (2, 5, 8), (3, 6, 9)] 2. [(1, 4, 8), (2, 5, 7), (3, 6, 9)] 3. [(1, 4, 9), (2, 5, 7), (3, 6, 8)] 4. [(1, 5, 7), (2, 4, 8), (3, 6, 9)] 5. ...
update:
thanks quick reply~!!
to clarify question:
the result non-repeated combinations of cartesian product of list a, b, c.
it can done ugly method:
1) generate whole list of cartesian product
from itertools import product, combinations, chain t = list(product(a, b, c))
2) using combinations generate possible results
p = list(combinations(t, 3))
3) filter repeated conditions
cnt = len(list(chain(a, b, c))) f = [x x in p if len(set(chain(*x))) == cnt]
update2:
expected result generated ugly method:
((1, 4, 7), (2, 5, 8), (3, 6, 9)) ((1, 4, 7), (2, 5, 9), (3, 6, 8)) ((1, 4, 7), (2, 6, 8), (3, 5, 9)) ((1, 4, 7), (2, 6, 9), (3, 5, 8)) ((1, 4, 8), (2, 5, 7), (3, 6, 9)) ((1, 4, 8), (2, 5, 9), (3, 6, 7)) ((1, 4, 8), (2, 6, 7), (3, 5, 9)) ((1, 4, 8), (2, 6, 9), (3, 5, 7)) ((1, 4, 9), (2, 5, 7), (3, 6, 8)) ((1, 4, 9), (2, 5, 8), (3, 6, 7)) ((1, 4, 9), (2, 6, 7), (3, 5, 8)) ((1, 4, 9), (2, 6, 8), (3, 5, 7)) ((1, 5, 7), (2, 4, 8), (3, 6, 9)) ((1, 5, 7), (2, 4, 9), (3, 6, 8)) ((1, 5, 7), (2, 6, 8), (3, 4, 9)) ((1, 5, 7), (2, 6, 9), (3, 4, 8)) ((1, 5, 8), (2, 4, 7), (3, 6, 9)) ((1, 5, 8), (2, 4, 9), (3, 6, 7)) ((1, 5, 8), (2, 6, 7), (3, 4, 9)) ((1, 5, 8), (2, 6, 9), (3, 4, 7)) ((1, 5, 9), (2, 4, 7), (3, 6, 8)) ((1, 5, 9), (2, 4, 8), (3, 6, 7)) ((1, 5, 9), (2, 6, 7), (3, 4, 8)) ((1, 5, 9), (2, 6, 8), (3, 4, 7)) ((1, 6, 7), (2, 4, 8), (3, 5, 9)) ((1, 6, 7), (2, 4, 9), (3, 5, 8)) ((1, 6, 7), (2, 5, 8), (3, 4, 9)) ((1, 6, 7), (2, 5, 9), (3, 4, 8)) ((1, 6, 8), (2, 4, 7), (3, 5, 9)) ((1, 6, 8), (2, 4, 9), (3, 5, 7)) ((1, 6, 8), (2, 5, 7), (3, 4, 9)) ((1, 6, 8), (2, 5, 9), (3, 4, 7)) ((1, 6, 9), (2, 4, 7), (3, 5, 8)) ((1, 6, 9), (2, 4, 8), (3, 5, 7)) ((1, 6, 9), (2, 5, 7), (3, 4, 8)) ((1, 6, 9), (2, 5, 8), (3, 4, 7))
what want not combinations indeed permutations. 3 elements have 6 permutations, cartesian product of 2 sets of permutations has 36. pm 2ring suspected want 3 of these permuted since question didn't tell otherwise. if code in question produces desired output, means want b
, c
permuted not a
. wrote code calculated permutations of a
, b
, c
. however, since a
doesn't need permuted, we'll wrap in list. gets close desired output:
import itertools = [1, 2, 3] b = [4, 5, 6] c = [7, 8, 9] in it.product([tuple(a)], it.permutations(b), it.permutations(c)): print(i)
the output 36 lines start with
((1, 2, 3), (4, 5, 6), (7, 8, 9)) ((1, 2, 3), (4, 5, 6), (7, 9, 8)) ((1, 2, 3), (4, 5, 6), (8, 7, 9))
it same format want indexes transposed o[x][y]
match o[y][x]
of desired output. use zip
magic transpose them. plus, function works number of arguments:
import itertools = [1, 2, 3] b = [4, 5, 6] c = [7, 8, 9] def funnyperms(first, *rest): in it.product([first], *(it.permutations(j) j in rest)): yield tuple(zip(*i)) in funnyperms(a, b, c): print(i)
the output
((1, 4, 7), (2, 5, 8), (3, 6, 9)) ((1, 4, 7), (2, 5, 9), (3, 6, 8)) ((1, 4, 8), (2, 5, 7), (3, 6, 9)) ((1, 4, 8), (2, 5, 9), (3, 6, 7)) ((1, 4, 9), (2, 5, 7), (3, 6, 8)) ((1, 4, 9), (2, 5, 8), (3, 6, 7)) ((1, 4, 7), (2, 6, 8), (3, 5, 9)) ((1, 4, 7), (2, 6, 9), (3, 5, 8)) ((1, 4, 8), (2, 6, 7), (3, 5, 9)) ((1, 4, 8), (2, 6, 9), (3, 5, 7)) ((1, 4, 9), (2, 6, 7), (3, 5, 8)) ((1, 4, 9), (2, 6, 8), (3, 5, 7)) ((1, 5, 7), (2, 4, 8), (3, 6, 9)) ((1, 5, 7), (2, 4, 9), (3, 6, 8)) ((1, 5, 8), (2, 4, 7), (3, 6, 9)) ((1, 5, 8), (2, 4, 9), (3, 6, 7)) ((1, 5, 9), (2, 4, 7), (3, 6, 8)) ((1, 5, 9), (2, 4, 8), (3, 6, 7)) ((1, 5, 7), (2, 6, 8), (3, 4, 9)) ((1, 5, 7), (2, 6, 9), (3, 4, 8)) ((1, 5, 8), (2, 6, 7), (3, 4, 9)) ((1, 5, 8), (2, 6, 9), (3, 4, 7)) ((1, 5, 9), (2, 6, 7), (3, 4, 8)) ((1, 5, 9), (2, 6, 8), (3, 4, 7)) ((1, 6, 7), (2, 4, 8), (3, 5, 9)) ((1, 6, 7), (2, 4, 9), (3, 5, 8)) ((1, 6, 8), (2, 4, 7), (3, 5, 9)) ((1, 6, 8), (2, 4, 9), (3, 5, 7)) ((1, 6, 9), (2, 4, 7), (3, 5, 8)) ((1, 6, 9), (2, 4, 8), (3, 5, 7)) ((1, 6, 7), (2, 5, 8), (3, 4, 9)) ((1, 6, 7), (2, 5, 9), (3, 4, 8)) ((1, 6, 8), (2, 5, 7), (3, 4, 9)) ((1, 6, 8), (2, 5, 9), (3, 4, 7)) ((1, 6, 9), (2, 5, 7), (3, 4, 8)) ((1, 6, 9), (2, 5, 8), (3, 4, 7))
storing these set , comparing values produced method proves have identical output:
print(set(funnyperms(a, b, c)) == set(f))
prints true
, q.e.d.
Comments
Post a Comment