I want to give the following:
(('A',), ('B',), ('C',), ('D',)) (('A',), ('B',), ('C','D')) (('A',), ('B','C'), ('D',)) (('A',), ('B','C','D')) (('A','B'), ('C',), ('D',)) (('A','B'), ('C','D')) (('A','B','C'), ('D',)) (('A','B','C','D'),)
when calling sub_combinations(('A', 'B', 'C', 'D'))
Here is my attempt, but it does not work:
def sub_combinations(segment): for i in range(1, len(segment)): for j in sub_combinations(segment[i:]): yield segment[:i]+j yield segment
but I think I'm on the right track.
In addition, I would like the second argument to be called a limit that limits the size of the subsets, for example sub_combinations(('A', 'B', 'C', 'D'), 2) :
(('A',), ('B',), ('C',), ('D',)) (('A',), ('B',), ('C','D')) (('A',), ('B','C'), ('D',)) (('A','B'), ('C',), ('D',)) (('A','B'), ('C','D'))
I am using python 3.