tricky nested list unpacking problem
Kirk Strauser
kirk at daycos.com
Mon Dec 15 15:02:42 EST 2008
At 2008-12-15T19:06:16Z, Reckoner <reckoner at gmail.com> writes:
> 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:
Recursion is your friend.
Write a function to unpack one "sublist" and call itself again with the new
list. For instance, something like:
def unpack(pattern):
# Find the first subpattern to replace
# [...]
results = []
for number in subpattern:
results.append(pattern.replace(subpattern, number))
return results
Calling unpack([1,2,3,[5,6],[7,8,9]]) would look cause it to call
unpack([1,2,3,5,[7,8,9]]) and unpack([1,2,3,6,[7,8,9]]), compile the
results, and return them.
--
Kirk Strauser
The Day Companies
More information about the Python-list
mailing list