tricky nested list unpacking problem

Kirk Strauser kirk at daycos.com
Mon Dec 15 15:24:14 EST 2008


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".
-- 
Kirk Strauser
The Day Companies



More information about the Python-list mailing list