Dec-03-2018, 04:25 PM
(This post was last modified: Dec-03-2018, 04:26 PM by Gribouillis.)
Your algorithm is ineffictient because at line 128, 129 you generate too many subsets and then you eliminate subsets having a non empty intersection. I think you can simplify this a lot
from itertools import chain, combinations, filterfalse def almost_powerset(iterable): "almost_powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3)" s = list(iterable) return chain.from_iterable(combinations(s, r) for r in range(len(s))) def con_ant(n): ind = list(range(1, n+1)) for s in almost_powerset(ind): b = set(s) t = [x for x in filterfalse(b.__contains__, ind)] yield s, t def build_lst_nums(num): lst = ["1." + str(x) for x in range(1, num + 1)] u = lst[:1] for s, t in con_ant(num-1): a = u + [lst[x] for x in s] b = [lst[x] for x in t] yield a, b yield lst[1:], u if __name__ == '__main__': for s, t in build_lst_nums(4): print(s, t)
Output:['1.1'] ['1.2', '1.3', '1.4']
['1.1', '1.2'] ['1.3', '1.4']
['1.1', '1.3'] ['1.2', '1.4']
['1.1', '1.4'] ['1.2', '1.3']
['1.1', '1.2', '1.3'] ['1.4']
['1.1', '1.2', '1.4'] ['1.3']
['1.1', '1.3', '1.4'] ['1.2']
['1.2', '1.3', '1.4'] ['1.1']