combinations




Pay Notebook Creator: Salah Ahmed0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0

Combinations of x items

{items_text: items ? separate each item with a new line, separate groups of items with '---'}

In [1]:
# Crosscompute
items_text_path = './items.txt'
target_folder = './results/'
In [68]:
from os.path import join
items = items_text_path
In [69]:
items = open(items_text_path, 'r').read()
In [70]:
import re
pattern = re.compile(r'-+')
lists = []
for l in pattern.split(items):
    lists.append([x.strip() for x in l.splitlines() if x.strip()])
npairs = len(lists)
In [71]:
from itertools import product, groupby
from operator import itemgetter
from pandas import DataFrame

possible_combinations = sorted(list(product(*lists)), key=itemgetter(0))
In [72]:
lists = groupby(possible_combinations, key=itemgetter(0))
i = 1
paths = []
for k, g in lists:
    target_path = join(target_folder, 'combos%d.csv' % i)
    paths.append(target_path)
    i += 1
    df = DataFrame(list(g))
    df.to_csv(target_path, index=False)
In [73]:
for i, p in enumerate(paths):
    print('combos%d_table_path = %s' % (i, p))
combos0_table_path = ./combos1.csv
combos1_table_path = ./combos2.csv
combos2_table_path = ./combos3.csv