Are there only a few, unchanging templates? If so, (dynamiclly) create a function for each template. This will be nearly the fastest you can go in Python, excluding the time to create and byte-compile the nesting function.
# This code is in the public domain
def make_nesting_expression(l, s):
result = []
for c in l:
if isinstance(c, list):
sub, s = make_nesting_expression(c, s)
result.append(sub)
else:
result.append("l[%d]" % s)
s += 1
print "make_nesting_expression", l, result
return "[" + ",".join(result) + "]", s
def make_nesting_function(l):
return eval("lambda l: %s" % make_nesting_expression(l, 0)[0])
t = [['a1','a2'],['b1'],['c1'],['d1']]
l = [1, 2, 3, 4, 5]
f = make_nesting_function(t)
print f(l)
Jeff
pgpntDRR9zF1S.pgp
Description: PGP signature
-- http://mail.python.org/mailman/listinfo/python-list
