-1

I am attempting to get all possible combinations of any number of series. How would I write a loop to get all the combinations and print them into an excel file?

Input

Series 1 A,B,C
Series 2 1,2,3
Series 3 X,Y,Z

Output

Combo 1 A,1,x

Combo 2 A,2,X

Combo 3 A,3,X

ect....

polonius11
  • 1,703
  • 5
  • 15
  • 23
  • What series? Be specific and provide some examples. – saga Jan 27 '17 at 21:49
  • This is the closest I got - however it is an excel formula. https://www.extendoffice.com/documents/excel/3097-excel-list-all-possible-combinations.html I am trying to recreate this in python. – polonius11 Jan 27 '17 at 21:52
  • 1
    Possible duplicate of [Get the cartesian product of a series of lists?](http://stackoverflow.com/questions/533905/get-the-cartesian-product-of-a-series-of-lists) – hansaplast Jan 27 '17 at 21:58
  • I added an answer below, but generally: please do some online research first before posting a new question. The way you ask sounds like "please solve my problem". If you provide *some* python code, e.g. just the three series as python lists, your question sounds more credible and you will get fewer downvotes – hansaplast Jan 27 '17 at 22:02

3 Answers3

2

Use itertools.product to find all possible combinations:

s1 = ['A', 'B', 'C']
s2 = [1,2,3]
s3 = ['X', 'Y', 'Z']
from itertools import combinations
combos = itertools.product(s1,s2,s3)

As for writing into an excel file, please see here.

Community
  • 1
  • 1
hansaplast
  • 11,007
  • 2
  • 61
  • 75
1

Run three for loops, one inside the other:

for a in s1:
    for b in s2:
        for c in s3:
             # do stuff. a, b, c is the combination
saga
  • 1,933
  • 2
  • 17
  • 44
0

Please see working solution below using xlwings.

import xlwings as xw

wb = xw.Book(r'C:\somepath')
sht = wb.sheets('Fields')
s1 = sht.range('A1:A10')
s2 = sht.range('B1:B12')

for a in s1:
    for b in s2:
        print (a.value + "," + b.value)
polonius11
  • 1,703
  • 5
  • 15
  • 23