views:

178

answers:

2

Say I have a dataset like

(1, 2, (3, 4), (5, 6), (7, 8, (9, 0)))

I want to convert it to a (semi) flat representation like,

(
(1, 2),
(1, 2, 3, 4),
(1, 2, 5, 6),
(1, 2, 7, 8),
(1, 2, 7, 8, 9, 0),
)

If you use this, (taken from SO)

def flatten(iterable):
    for i, item in enumerate(iterable):
        if hasattr(item, '__iter__'):
            for nested in flatten(item):
                yield nested
        else:
            yield item

this will convert it to a list like(after iterating)

[1, 2, 3, 4, 5, 6, 7, 8, 9]

But I cant get the original from this reperenstation, while I can get the original back from the first. (If every tuple has 2 elements only)

A: 

How about a using a different "flat" representation, one which can be converted back:

[1, 2, '(', 3, 4, ')', '(', 5, 6, ')', '(', 7, 8, '(', 9, 0, ')', ')']
yairchu
+2  A: 

This will give the example output. Don't know if that's really the best way of representing the model you want, though...

def combineflatten(seq):
    items= tuple(item for item in seq if not isinstance(item, tuple))
    yield items
    for item in seq:
        if isinstance(item, tuple):
            for yielded in combineflatten(item):
                yield items+yielded

>>> tuple(combineflatten((1, 2, (3, 4), (5, 6), (7, 8, (9, 0)))))
((1, 2), (1, 2, 3, 4), (1, 2, 5, 6), (1, 2, 7, 8), (1, 2, 7, 8, 9, 0))
bobince