D
DJTB
Hi all,
I'm new to Python. I'm trying to create a fast function to do the following:
t = [['a1','a2'],['b1'],['c1'],['d1']]
l = [1,2,3,4,5][[1, 2], [3], [4], [5]]
t is some sort of template. This is what I have now:
def create_nested_list(template,l_orig):
'''Uses a template to create a new list
t = [['a1','a2'],['b1'],['c1'],['d1']]
l = [1,2,3,4,5] [[1, 2], [3], [4], [5]]
'''
tl = map(len, template)
# Input size check
if reduce(lambda x,y: x+y, tl) != len(l_orig):
raise "Wrong input size"
l = l_orig
new_nested_list = []
for x in tl:
q = []
i = 0
while i < x:
q.append(l.pop(0))
i += 1
new_nested_list.append(q)
return new_nested_list
I'd like to know if it is possible to make this faster (using Python magic I
don't know of yet), because this function will be called a lot
('constantly').
Thanks in advance,
Stan.
I'm new to Python. I'm trying to create a fast function to do the following:
t = [['a1','a2'],['b1'],['c1'],['d1']]
l = [1,2,3,4,5][[1, 2], [3], [4], [5]]
t is some sort of template. This is what I have now:
def create_nested_list(template,l_orig):
'''Uses a template to create a new list
t = [['a1','a2'],['b1'],['c1'],['d1']]
l = [1,2,3,4,5] [[1, 2], [3], [4], [5]]
'''
tl = map(len, template)
# Input size check
if reduce(lambda x,y: x+y, tl) != len(l_orig):
raise "Wrong input size"
l = l_orig
new_nested_list = []
for x in tl:
q = []
i = 0
while i < x:
q.append(l.pop(0))
i += 1
new_nested_list.append(q)
return new_nested_list
I'd like to know if it is possible to make this faster (using Python magic I
don't know of yet), because this function will be called a lot
('constantly').
Thanks in advance,
Stan.