itertools - Python permutations of both sequence and subsequences -
question: how implement double_permutations(s) below?
>>> s = [('a', 'b'), ('c', 'd'), ('e', 'f')] >>> answer in double_permutation(s): ... print(answer) # in order [('a', 'b'), ('c', 'd'), ('e', 'f')] [('a', 'b'), ('d', 'c'), ('e', 'f')] [('a', 'b'), ('c', 'd'), ('f', 'e')] [('a', 'b'), ('d', 'c'), ('f', 'e')] [('a', 'b'), ('e', 'f'), ('c', 'd')] [('a', 'b'), ('f', 'e'), ('c', 'd')] [('a', 'b'), ('e', 'f'), ('d', 'c')] [('a', 'b'), ('f', 'e'), ('d', 'c')] what i've tried (breaks down once outer list longer 3 elements)
from itertools import permutations def double_permutation(l): def double_permutation_recur(s, r): if not r: yield s else: permutation in permutations(r): s1 = s + [permutation[0]] s2 = s + [(permutation[0][1], permutation[0][0])] perm1 in double_permutation_recur(s1, permutation[1:]): yield perm1 perm2 in double_permutation_recur(s2, permutation[1:]): yield perm2 return double_permutation_recur([l[0]], l[1:]) this should yield double_factorial(n-1) answers list of length n. works through n = 3, breaks down @ n = 4 (which yields 96 instead of 48 answers).
you can build primitives in itertools module
import itertools s = [('a', 'b'), ('c', 'd'), ('e', 'f')] is you're describing?
def permute(it): return itertools.product(*(itertools.permutations(i) in it)) >>> in permute(s): ... print (('a', 'b'), ('c', 'd'), ('e', 'f')) (('a', 'b'), ('c', 'd'), ('f', 'e')) (('a', 'b'), ('d', 'c'), ('e', 'f')) (('a', 'b'), ('d', 'c'), ('f', 'e')) (('b', 'a'), ('c', 'd'), ('e', 'f')) (('b', 'a'), ('c', 'd'), ('f', 'e')) (('b', 'a'), ('d', 'c'), ('e', 'f')) (('b', 'a'), ('d', 'c'), ('f', 'e')) or want:
def permute2(it): return itertools.chain.from_iterable( permute(p) p in itertools.permutations(it) ) >>> in permute2(s): ... print (('a', 'b'), ('c', 'd'), ('e', 'f')) (('a', 'b'), ('c', 'd'), ('f', 'e')) (('a', 'b'), ('d', 'c'), ('e', 'f')) (('a', 'b'), ('d', 'c'), ('f', 'e')) (('b', 'a'), ('c', 'd'), ('e', 'f')) (('b', 'a'), ('c', 'd'), ('f', 'e')) (('b', 'a'), ('d', 'c'), ('e', 'f')) (('b', 'a'), ('d', 'c'), ('f', 'e')) (('a', 'b'), ('e', 'f'), ('c', 'd')) (('a', 'b'), ('e', 'f'), ('d', 'c')) (('a', 'b'), ('f', 'e'), ('c', 'd')) (('a', 'b'), ('f', 'e'), ('d', 'c')) (('b', 'a'), ('e', 'f'), ('c', 'd')) (('b', 'a'), ('e', 'f'), ('d', 'c')) (('b', 'a'), ('f', 'e'), ('c', 'd')) (('b', 'a'), ('f', 'e'), ('d', 'c')) (('c', 'd'), ('a', 'b'), ('e', 'f')) (('c', 'd'), ('a', 'b'), ('f', 'e')) (('c', 'd'), ('b', 'a'), ('e', 'f')) (('c', 'd'), ('b', 'a'), ('f', 'e')) (('d', 'c'), ('a', 'b'), ('e', 'f')) (('d', 'c'), ('a', 'b'), ('f', 'e')) (('d', 'c'), ('b', 'a'), ('e', 'f')) (('d', 'c'), ('b', 'a'), ('f', 'e')) (('c', 'd'), ('e', 'f'), ('a', 'b')) (('c', 'd'), ('e', 'f'), ('b', 'a')) (('c', 'd'), ('f', 'e'), ('a', 'b')) (('c', 'd'), ('f', 'e'), ('b', 'a')) (('d', 'c'), ('e', 'f'), ('a', 'b')) (('d', 'c'), ('e', 'f'), ('b', 'a')) (('d', 'c'), ('f', 'e'), ('a', 'b')) (('d', 'c'), ('f', 'e'), ('b', 'a')) (('e', 'f'), ('a', 'b'), ('c', 'd')) (('e', 'f'), ('a', 'b'), ('d', 'c')) (('e', 'f'), ('b', 'a'), ('c', 'd')) (('e', 'f'), ('b', 'a'), ('d', 'c')) (('f', 'e'), ('a', 'b'), ('c', 'd')) (('f', 'e'), ('a', 'b'), ('d', 'c')) (('f', 'e'), ('b', 'a'), ('c', 'd')) (('f', 'e'), ('b', 'a'), ('d', 'c')) (('e', 'f'), ('c', 'd'), ('a', 'b')) (('e', 'f'), ('c', 'd'), ('b', 'a')) (('e', 'f'), ('d', 'c'), ('a', 'b')) (('e', 'f'), ('d', 'c'), ('b', 'a')) (('f', 'e'), ('c', 'd'), ('a', 'b')) (('f', 'e'), ('c', 'd'), ('b', 'a')) (('f', 'e'), ('d', 'c'), ('a', 'b')) (('f', 'e'), ('d', 'c'), ('b', 'a')) or "anchor" first element:
def permute3(s): return s[:1] + list(p) p in permute2(s[1:]) >>> in permute3(s): ... print [('a', 'b'), ('c', 'd'), ('e', 'f')] [('a', 'b'), ('c', 'd'), ('f', 'e')] [('a', 'b'), ('d', 'c'), ('e', 'f')] [('a', 'b'), ('d', 'c'), ('f', 'e')] [('a', 'b'), ('e', 'f'), ('c', 'd')] [('a', 'b'), ('e', 'f'), ('d', 'c')] [('a', 'b'), ('f', 'e'), ('c', 'd')] [('a', 'b'), ('f', 'e'), ('d', 'c')]
Comments
Post a Comment