eval ? ['LBBW': checked]

Holger Joukl Holger.Joukl at LBBW.de
Fri Feb 20 09:06:28 EST 2004


You can use the exec statement (note that I set the if-condition to
1=true for the example, and I used different names for outer and inner
loop indexes):

>>> for index in range(1,4):
...     list = []
...     for inner in range(1,7):
...             if 1:
...                     list.append(inner)
...     foo = "list_" + str(index) + "= list"
...     exec foo
...
>>> list_1
[1, 2, 3, 4, 5, 6]
>>> list_2
[1, 2, 3, 4, 5, 6]
>>> dir()
['__builtins__', '__doc__', '__name__', 'foo', 'index', 'inner', 'list',
'list_1', 'list_2', 'list_3']
>>> globals()
{'list_3': [1, 2, 3, 4, 5, 6], 'index': 3, 'list_1': [1, 2, 3, 4, 5, 6],
'list_2': [1, 2, 3, 4, 5, 6], '__builtins__': <module '__builtin__'
(built-in)>, 'list': [1, 2, 3, 4, 5, 6], 'inner': 6, '__name__':
'__main__', 'foo': 'list_3= list', '__doc__': None}

__________________



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

--
http://mail.python.org/mailman/listinfo/python-list



+----------------------------------------------------------------------+
| LBBW Security Information                                            |
+----------------------------------------------------------------------+
| - Die Nachricht war weder verschluesselt noch digital unterschrieben |
+----------------------------------------------------------------------+


Der Inhalt dieser E-Mail ist vertraulich. Falls Sie nicht der angegebene
Empfänger sind oder falls diese E-Mail irrtümlich an Sie adressiert wurde,
verständigen Sie bitte den Absender sofort und löschen Sie die E-Mail
sodann. Das unerlaubte Kopieren sowie die unbefugte Übermittlung sind nicht
gestattet. Die Sicherheit von Übermittlungen per E-Mail kann nicht
garantiert werden. Falls Sie eine Bestätigung wünschen, fordern Sie bitte
den Inhalt der E-Mail als Hardcopy an.

The contents of this  e-mail are confidential. If you are not the named
addressee or if this transmission has been addressed to you in error,
please notify the sender immediately and then delete this e-mail.  Any
unauthorized copying and transmission is forbidden. E-Mail transmission
cannot be guaranteed to be secure. If verification is required, please
request a hard copy version.







More information about the Python-list mailing list