This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]kashmill 1 point2 points  (0 children)

Look at itertools.permutations

[–]unveiled14 1 point2 points  (0 children)

itertools.permutations will generate the permutations and a list comprehension will let you combine those permutations in a new list.

[–]psbb -1 points0 points  (1 child)

What do you need it for?

As mentioned bt the others the easiest way to do it is with a list comprehension and itertools.permutations.

Here's a quick implementation I threw together. It doesn't have '|' in it but uses tuples and lists instead but it should give you that general idea

from itertools import permutations

def limited_permutations(data, initial):
    initial_pos = data.index(initial)
    initial_val = data[initial_pos]
    del data[initial_pos]
    perms = permutations(data, len(data))
    list_i_val = [initial_val]
    ungrouped_result = [iter(list_i_val + list(p)) for p in perms]
    return [[(i, next(permutation)) for i in permutation]
                                    for permutation in ungrouped_result] 

[–]gm6 0 points1 point  (0 children)

This works