1

With

import itertools
l1 = [1,2,3,4]
l2 = [1,2,3,4]

x = itertools.product(l1, l2)
print(list(x))

I get the 16 possible individual combinations of the lists l1 and l2, [(1,1), (1,2), ... ] etc. Is there a way to also get combinations that 'connect' the lists in multiple ways? For example, I could have the following combinations [(1,1), (2,3)], [(1,3), (2,4), (3,2)], and [(1,2), (2,4), (3,3), (4, 1)].

Any help would be greatly appreciated.

  • What do you mean 'connect' the lists in multiple ways? From your examples I can't make out what you're trying to do – user15270287 Mar 01 '21 at 14:46

2 Answers2

0

is this what you are trying to achieve?

from itertools import permutations
l1 = [1,2,3,4]
l2 = [1,2,3,4]

for i in l1:
    a= permutations(l2,i)
    for m in list(a):
        print(m)
Ade_1
  • 1,480
  • 1
  • 6
  • 17
0

You can view the problem as placing l1 balls, into l2 bins. See this answer on how to generate all those placements.

Because in your example, there are placements where some of the balls are absent from any bin, we can handle that by introducing an additional bin which we designate to be the trash bin (in other words, balls which end up in this bin, will be considered absent).

wsdookadr
  • 2,584
  • 1
  • 21
  • 44
  • This sounds like it could be the way to go, I think I'm just bad at explaining things. The way I'm thinking of it is two graphs with 4 nodes; the problem then becomes: what are all the possible combinations of nodes that connect the two graphs, only using each node once or not at all? – P. H. Allus Mar 01 '21 at 15:32
  • Sure, you can also view the problem like that, in which case you are counting [bipartite graphs](https://en.wikipedia.org/wiki/Bipartite_graph). The solution above is equivalent and should give you what you need. – wsdookadr Mar 01 '21 at 15:34