I am not sure of the proper mathematical terminology for the code I'm trying to write. I would like to generate combinations of unique integers where the "ordered subsets" of each combination are used to exclude some later combinations.
Hope the example will be clear:
from itertools import chain, combinations mylist = range(4) max_depth = 3 rev = chain.from_iterable(combinations(mylist, i) for i in xrange(max_depth, 0, -1)) for el in list(rev): print el
This code outputs an output containing all the subsets I want, but also some additional ones that I don't have. I manually inserted comments to indicate which items I do not want.
(0, 1, 2) (0, 1, 3) (0, 2, 3) (1, 2, 3) (0, 1)
So the desired output of my generator or iterator would be:
(0, 1, 2) (0, 1, 3) (0, 2, 3) (1, 2, 3) (0, 3) (1, 3) (2, 3) (3,)
I know that I can make a list of all (desired and undesirable) combinations, and then filter out the ones that I don't need, but I was wondering if there was a more efficient, generator or iterative method.