-3

I have three different lists

a = [139.8, 4.2]
b = [101.6, 114.3, 4.0, 139.8, 4.2]
c = [60.5, 4.0, 89.1, 5.7, 101.6, 4.0]
for i in range....
     for.... #For first combination
            get1 = 139.8
            get2 = 101.6
            get3 = 60.5

Expected output combinations:

First combination : 139.8, 101.6, 60.5

Second combination: 139.8, 114.3, 4.0

#Edit Thrid combination: 139.8, 4.0, 89.1 and so on....

I want to use a loop to get ALL the combinations of three items from each list one after another. I couldn't develop the logic for this. How can this be done?

devinxxd
  • 5
  • 1
  • 5

1 Answers1

1

It seems you're looking for itertools.product().

>>> a = [139.8, 4.2]
>>> b = [101.6, 114.3, 4.0, 139.8, 4.2]
>>> c = [60.5, 4.0, 89.1, 5.7, 101.6, 4.0]
>>> import itertools
>>> import pprint
>>> pprint.pprint(list(itertools.product(a,b,c)))
[(139.8, 101.6, 60.5),
 (139.8, 101.6, 4.0),
 (139.8, 101.6, 89.1),
 (139.8, 101.6, 5.7),
 (139.8, 101.6, 101.6),
 (139.8, 101.6, 4.0),
 (139.8, 114.3, 60.5),
...
 (4.2, 139.8, 101.6),
 (4.2, 139.8, 4.0),
 (4.2, 4.2, 60.5),
 (4.2, 4.2, 4.0),
 (4.2, 4.2, 89.1),
 (4.2, 4.2, 5.7),
 (4.2, 4.2, 101.6),
 (4.2, 4.2, 4.0)]
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561