By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,008 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

eval ?

P: n/a

I'm trying to use eval (is the right function? ) to generate empty lists
with different names(es. list_1, list_2, list_3, ...) in a loop similar
to:

for index in range(1,4):
list=[]
for index in range(1,7):
if <condition>:
list.append(1)
foo='list_'+str(index)+'=list'
eval(foo)

I am not a programmer as you probably see from the code and I do not
even know if this is the right approach to do that in Python (I used
this structure with Matlab that I want now to dismiss ...)

Any help?
Thanks
angelo

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <ma**************************************@python.o rg>,
Angelo Secchi <se****@sssup.it> wrote:

I'm trying to use eval (is the right function? ) to generate empty lists
with different names(es. list_1, list_2, list_3, ...) in a loop similar
to:

for index in range(1,4):
list=[]
for index in range(1,7):
if <condition>:
list.append(1)
foo='list_'+str(index)+'=list'
eval(foo)

I am not a programmer as you probably see from the code and I do not
even know if this is the right approach to do that in Python (I used
this structure with Matlab that I want now to dismiss ...)

Jul 18 '05 #2

P: n/a
One way to do this is:

for index in range(1,4):
list=[]
for index in range(1,7):
if <condition>:
list.append(1)
locals()['list_'+str(index)] = list

Of course this isn't going to work the way you expect, because you're
assigning a pointer to "list" to "list_n" with each iteration, and at the
end, all of your "list_n" variables are going to reference the same object,
the one orginally referenced by "list"

Perhaps you are looking for something like:
dict([('list_'+str(n), [1,]*n) for n in xrange(1,7) if <condition>])
so if condition is "n in (2,4,6)", you'd get output like:
foo = dict([('list_'+str(n), [1]*n) for n in xrange(1,7) if n in (2,4,6)]) foo {'list_2': [1, 1], 'list_6': [1, 1, 1, 1, 1, 1], 'list_4': [1, 1, 1, 1]} foo['list_4']
[1, 1, 1, 1]

Note that I'm createing a brand new list (via "[1]*n") with each iteration
of the list comprehension, rather than using a reference to a previously
created list.

HTH
Chris

"Angelo Secchi" <se****@sssup.it> wrote in message
news:ma**************************************@pyth on.org...
I'm trying to use eval (is the right function? ) to generate empty lists
with different names(es. list_1, list_2, list_3, ...) in a loop similar
to:

for index in range(1,4):
list=[]
for index in range(1,7):
if <condition>:
list.append(1)
foo='list_'+str(index)+'=list'
eval(foo)

I am not a programmer as you probably see from the code and I do not
even know if this is the right approach to do that in Python (I used
this structure with Matlab that I want now to dismiss ...)

Any help?
Thanks
angelo

Jul 18 '05 #3

P: n/a
In article <ma**************************************@python.o rg>,
Angelo Secchi <se****@sssup.it> wrote:

I'm trying to use eval (is the right function? ) to generate empty lists
with different names(es. list_1, list_2, list_3, ...) in a loop similar
to:

for index in range(1,4):
list=[]
for index in range(1,7):
if <condition>:
list.append(1)
foo='list_'+str(index)+'=list'
eval(foo)

I am not a programmer as you probably see from the code and I do not
even know if this is the right approach to do that in Python (I used
this structure with Matlab that I want now to dismiss ...)


I believe you want exec . Do you mean something like:

for i in range (1, 4+1):
li = []
for j in range (1, 7+1):
if (condition):
li.append (1)
exec ("list_%d%d = li[:]" % (i, j,))

(I've improvised out of a few possible problems.
`list` is already used in Python as the type for lists.
It's better to name an actual list something else: `li` for
instance.
It's possible to use `index` for two different things, as
you've done, but frequently each use needs its own name, as
here.
Each run through the `i` loop creates a distinct list,
which is given the name `li`. In the `j` loop this distinct
list is appended to without changing its identity. Unless
you apply your new name to a copy of that list, all your
`list_1x` names will apply to the same list instance.. as will
all your `list_2x` names, all your `list_3x` names, etc. The
`li[:]` construct copies the contents of li into a fresh
list instance each time. This may (or may not) be what you
wanted.)

Regards. Mel.
Jul 18 '05 #4

P: n/a
Hi Angelo,

Welcome to Python =)

Why not make a list of lists? I.e.

lists = [ [] for i in range(3) ]
for my_list in lists:
for i in range(6):
if some_condition(i):
my_list.append(1)

It's generally best to avoid eval if you don't need it. And some tips-

1. Don't use "list" as a variable name. It's a builtin type!

2. Iterate over objects in lists directly, not with an index, unless you
actually need the index for something. Example,
for obj in obj_list:
print obj

instead of

for i in range(len(obj_list)):
print obj_list[i]

3. Don't use the same variable name "index" for both for loops.

4. String formatters!
"list%s = my_list" % index
is more readable than
"list" + str(index) + " = my_list"
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.