3

I have about 12 lists [a, b, c, ... , z] with arbitrary elements and i´ve got a series of combinations through itertools.combinations(iterable, n) resulting in lists of combinations that match each of the original list.

The great deal now is to get a list with all the possible combinations, picking one element(combination) of each combinations list.

One simplified exemple would be:

A = [a,b,c]
B = [d,e,f]
C = [g,h,i]

my_iterable = [A, B, C]

And the output should be:

>>> foo(my_iterable)
(a,d,g), (a,d,h), (a,d,i), (a,e,g), (a,e,h), ... , (c,f,i)

The input iterables, e.g. 'A, B & C', may have variable lengths and foo() may be a generator function.

Vibhutha Kumarage
  • 1,372
  • 13
  • 26
Pedro
  • 1,121
  • 7
  • 16

1 Answers1

1
A = ['a','b','c']
B = ['d','e','f']
C = ['g','h','i']

l = [(a, b, c) for a in A for b in B for c in C]
print(l)

out:

[('a', 'd', 'g'), ('a', 'd', 'h'), ('a', 'd', 'i'), ('a', 'e', 'g'), ('a', 'e', 'h'), ('a', 'e', 'i'), ('a', 'f', 'g'), ('a', 'f', 'h'), ('a', 'f', 'i'), ('b', 'd', 'g'), ('b', 'd', 'h'), ('b', 'd', 'i'), ('b', 'e', 'g'), ('b', 'e', 'h'), ('b', 'e', 'i'), ('b', 'f', 'g'), ('b', 'f', 'h'), ('b', 'f', 'i'), ('c', 'd', 'g'), ('c', 'd', 'h'), ('c', 'd', 'i'), ('c', 'e', 'g'), ('c', 'e', 'h'), ('c', 'e', 'i'), ('c', 'f', 'g'), ('c', 'f', 'h'), ('c', 'f', 'i')]
宏杰李
  • 11,820
  • 2
  • 28
  • 35