I based my original function on this post, this post, and this post. But it's not working. The main gotcha I'm getting is that even when I put tuple(c) in Example Two, I still get a list back.
I have two examples, one with nested tuples (the way it should be) and one with nested tuples in a list so that you can easily see the extra enclosure since there are too many parenthesis with nested tuples.
def example_one():
list_one = ((1, 2), (3, 4), (5, 6), (7, 8), (9, 10))
list_two = []
print "List One: " + str(list_one)
for i in range(0, 5):
c_tuple = tuple(c for c in itertools.combinations(list_one[:i], i))
list_two.append(c_tuple)
list_two = tuple(list_two)
print "List Two: " + str(list_two)
Outputs:
List One: ((1, 2), (3, 4), (5, 6), (7, 8), (9, 10))
List Two: (((),), (((1, 2),),), (((1, 2), (3, 4)),), (((1, 2), (3, 4), (5, 6)),), (((1, 2), (3, 4), (5, 6), (7, 8)),))
and
def example_two():
list_one = ((1, 2), (3, 4), (5, 6), (7, 8), (9, 10))
list_two = []
print "List One: " + str(list_one)
for i in range(0, 5):
c_tuple = [tuple(c) for c in itertools.combinations(list_one[:i], i)]
list_two.append(c_tuple)
list_two = tuple(list_two)
print "List Two: " + str(list_two)
Outputs:
List One: ((1, 2), (3, 4), (5, 6), (7, 8), (9, 10))
List Two: ([()], [((1, 2),)], [((1, 2), (3, 4))], [((1, 2), (3, 4), (5, 6))], [((1, 2), (3, 4), (5, 6), (7, 8))])
In the second example, the parenthesis represent the tuple in example one that I am trying to eliminate. In fact, to be honest, I am not sure if it is that tuple or the one inside that. Whichever one is being added unnecessarily I guess.
DESIRED OUTPUT:
List One: ((1, 2), (3, 4), (5, 6), (7, 8), (9, 10))
List Two: ((()), ((1, 2)), ((1, 2), (3, 4)), ((1, 2), (3, 4), (5, 6)), ((1, 2), (3, 4), (5, 6), (7, 8)))