eval ? ['LBBW': checked]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Holger Joukl

    eval ? ['LBBW': checked]

    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):
    [color=blue][color=green][color=darkred]
    >>> for index in range(1,4):[/color][/color][/color]
    .... list = []
    .... for inner in range(1,7):
    .... if 1:
    .... list.append(inn er)
    .... foo = "list_" + str(index) + "= list"
    .... exec foo
    ....[color=blue][color=green][color=darkred]
    >>> list_1[/color][/color][/color]
    [1, 2, 3, 4, 5, 6][color=blue][color=green][color=darkred]
    >>> list_2[/color][/color][/color]
    [1, 2, 3, 4, 5, 6][color=blue][color=green][color=darkred]
    >>> dir()[/color][/color][/color]
    ['__builtins__', '__doc__', '__name__', 'foo', 'index', 'inner', 'list',
    'list_1', 'list_2', 'list_3'][color=blue][color=green][color=darkred]
    >>> globals()[/color][/color][/color]
    {'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

    --




    +----------------------------------------------------------------------+
    | 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.




Working...