python - Generate a subset of possible permutations -
lets have 2 lists of lists , wand build various permutations it. @ these 2 lists:
l1 = [[0, 8], [4], [6, 7], [1], [3], [5], [2]] l2 = [[4, 8], [0], [6, 7], [3], [1], [5], [2]] the 2 lists represent mappings, have. know parts of permutation not of them. see it, there 4 permutations possible:
(4, 3, 2, 1, 0, 5, 6, 7, 8) (8, 3, 2, 1, 0, 5, 7, 6, 4) (4, 3, 2, 1, 0, 5, 7, 6, 8) (8, 3, 2, 1, 0, 5, 6, 7, 4) after thinking have following idea. generate 4 possibilities of l1 (being tuple in end) make l2 tuple, dictionary zip, order dictionary , convert these 4 dicts list of tuples. sound reasonable?
have hard time generating 4 tuples. have far.
l1 = [[0, 8], [4], [6, 7], [1], [3], [5], [2]] l2 = [[4, 8], [0], [6, 7], [3], [1], [5], [2]] out = [()] def get_permutations(lst): in lst: perms in permutations(i): out[0] = out[0] + perms return out print(get_permutations(l1)) which prints [(0, 8, 8, 0, 4, 6, 7, 7, 6, 1, 3, 5, 2)] how generate tuple when entry in list more 1 permutation of list entry possible?
i know print(list(itertools.product(*l1))) want not quite. yields [(0, 4, 6, 1, 3, 5, 2), (0, 4, 7, 1, 3, 5, 2), (8, 4, 6, 1, 3, 5, 2), (8, 4, 7, 1, 3, 5, 2)]. maybe there way modify that.
it not homework question.
provide context: building program test graph isomorphism. lists color classes of nodes. these color classes want limit possible permutations of graph few make brute-force quicker.
i take @ itertools. https://docs.python.org/3.5/library/itertools.html#itertools.permutations
Comments
Post a Comment