tricky nested list unpacking problem

Reckoner reckoner at gmail.com
Mon Dec 15 14:06:16 EST 2008


Hi,

I have lists of the following type:

[1,2,3,[5,6]]

and I want to produce the following strings from this as

'0-1-2-3-5'
'0-1-2-3-6'

That was easy enough. The problem is that these can be nested. For
example:

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

which should produce

'0-1-2-3-5-7'
'0-1-2-3-5-8'
'0-1-2-3-5-9'
'0-1-2-3-6-7'
'0-1-2-3-6-8'
'0-1-2-3-6-9'

also,

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

should produce

'0-1-2-3-5-7-9'
'0-1-2-3-6-7-9'

obviously, these are nested loops over the lists. The problem is that
I don't know ahead of time how many lists there are or how deep they
go. In other words, you could have:

[1,2,3,[5,6,[10, 11]],7,[9,[1, 2, 3, 4, 5 ]]]

Any help appreciated. I've really been having trouble with this.

I hope that made sense.




More information about the Python-list mailing list