0

Given two lists in Python

a = [1, 2, 3]
b = [4, 5, 6]

We can use c=list(itertools.product(a,b)) to generate all possible permutations, this will output

[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]

Now we need to generate these sets from c

[[(1,4),(2,5),(3,6)],
 [(1,4),(2,6),(3,5)],
 [(1,5),(2,4),(3,6)],
 [(1,5),(2,6),(3,4)],
 [(1,6),(2,4),(3,5)],
 [(1,6),(2,5),(3,4)]]

Each list here is a possible pairing of all elements in a with b. The number of pairings is the number of possible permutations taking two at a time. The solution needs work for any number of elements in a and b.

S_S
  • 1,276
  • 4
  • 24
  • 47
  • 1
    `[list(zip(x,b)) for x in itertools.permutations(a,len(b))]` - this does as shown in https://stackoverflow.com/a/12935562/3169868, thanks! – S_S Aug 19 '22 at 17:15
  • What you are actually trying to do is get the permutations of `b`, and then use `a` to "label" them (or perhaps vice-versa). – Karl Knechtel Mar 02 '23 at 01:21

0 Answers0