I have a list of items, e.g. [1,1,1,1,2,2]
, and I am trying to find all the unique groups where these items are bundled into tuples of length one or two. For example, for the group above, I would like to find the following 10 possible groupings:
[[(1,),(1,),(1,),(1,),(2,),(2,)],
[(1,1),(1,),(1,),(2,),(2,)],
[(1,2),(1,),(1,),(1,),(2,)],
[(2,2),(1,),(1,),(1,),(1,)],
[(1,1),(1,1),(2,),(2,)],
[(1,1),(1,2),(1,),(2,)],
[(1,2),(1,2),(1,),(1,)],
[(2,2),(1,1),(1,),(1,)],
[(1,1),(1,1),(2,2)],
[(1,1),(1,2),(1,2)]]
I've been playing with itertools to but can only manage to use it to find unique possible tuples (e.g. set(list(itertools.combinations((1,1,1,1,2,2),2)))
) and any searches I do pop up solutions where the size of each group is constant and/or duplication of elements is not considered (example1, example2).
Ultimately, I am looking for a solution that will work for cases that are all ones ([1,1,1,...,1]
), all twos ([2,2,2,...,2]
) or some intermediate combination that includes an arbitrary number of 1s and 2s.