tricky nested list unpacking problem

Chris Rebert clp at rebertia.com
Mon Dec 15 20:13:25 EST 2008


On Mon, Dec 15, 2008 at 12:24 PM, Kirk Strauser <kirk at daycos.com> wrote:
> At 2008-12-15T20:03:14Z, "Chris Rebert" <clp at rebertia.com> writes:
>
>> You just need a recursive list-flattening function. There are many
>> recipes for these. Here's mine:
>
>>>>> flattened = flatten([1,2,3,[5,6,[10, 11]],7,[9,[1, 2, 3, 4, 5 ]]])
>>>>> flattened
>> [1, 2, 3, 5, 6, 10, 11, 7, 9, 1, 2, 3, 4, 5]
>>>>> '-'.join(str(num) for num in flattened)
>> '1-2-3-5-6-10-11-7-9-1-2-3-4-5'
>
> He doesn't want to flatten them directly.  He's using [1,2,3] sort of like a
> regular expression, so that 1,[2,3],4 means "1,2,4" or "1,3,4", not
> "1,2,3,4".

Ah, my bad. Misinterpreted. Still, it's a similar principle involved.
He just needs to code up the right recursive function. Not all that
hard.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list