[Tutor] create and fill a list with exec?
michaelbaker@operamail.com
michaelbaker@operamail.com
Fri, 09 Feb 2001 13:38:35 -0800
how can I create an empty list and then fill it with stuff at the time of
creation or without calling it explicitly by name later?
>>> dir()
['__builtins__', '__doc__', '__name__']
>>> a=[1,2,3,4,5]
>>> for b in a:
exec 'buffer%s=[]' %b
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'b', 'buffer1', 'buffer2',
'buffer3', 'buffer4', 'buffer5']
I want to do someting like this:
>>> a=[1,2,3,4,5]
>>> for b in a:
exec 'buffer%s=[], buffer%s.append(b)' %b
this doesn't work.
I could fill these lists later, but I'm not sure how to do that either:
>>> for c in dir():
for d in range(len(a)+1):
if c=='buffer%s' %d:
c.append(d)
Traceback (innermost last):
File "<pyshell#27>", line 4, in ?
c.append(d)
AttributeError: append
how can I make this work???
thanks tutors