[exec cmd for cmd in cmds]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Schüle Daniel

    #1

    [exec cmd for cmd in cmds]

    Hello all,
    [color=blue][color=green][color=darkred]
    >>> p = "z%i = complex(1-1e-%i, 1-1e-%i)"
    >>> lst = [p % (i,i,i) for i in range(10, 30)]
    >>> for item in lst:[/color][/color][/color]
    .... exec item
    ....[color=blue][color=green][color=darkred]
    >>>
    >>> p = "z%i = complex(1-1e-%i, 1-1e-%i)"
    >>> lst = [p % (i,i,i) for i in range(10, 30)]
    >>> [exec item for item in lst][/color][/color][/color]
    File "<stdin>", line 1
    [exec item for item in lst]
    ^
    SyntaxError: invalid syntax[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    is this prohibited for some reasons or is this just happens to be
    disallowed?


    this is one more cool way[color=blue][color=green][color=darkred]
    >>> p = "z%i = complex(1-1e-%i, 1-1e-%i);"
    >>> c = reduce(lambda x,y: x+y, [p % (i,i,i) for i in range(20,30)])
    >>> exec c[/color][/color][/color]

    and one more :)[color=blue][color=green][color=darkred]
    >>> p = "z%i = complex(1-1e-%i, 1-1e-%i);"
    >>> c = "".join([ p % (i,i,i) for i in range(20,30) ])
    >>> exec c[/color][/color][/color]

    Regards, Daniel

  • Diez B. Roggisch

    #2
    Re: [exec cmd for cmd in cmds]

    Schüle Daniel wrote:
    [color=blue]
    > Hello all,
    >[color=green][color=darkred]
    > >>> p = "z%i = complex(1-1e-%i, 1-1e-%i)"
    > >>> lst = [p % (i,i,i) for i in range(10, 30)]
    > >>> for item in lst:[/color][/color]
    > ... exec item
    > ...[color=green][color=darkred]
    > >>>
    > >>> p = "z%i = complex(1-1e-%i, 1-1e-%i)"
    > >>> lst = [p % (i,i,i) for i in range(10, 30)]
    > >>> [exec item for item in lst][/color][/color]
    > File "<stdin>", line 1
    > [exec item for item in lst]
    > ^
    > SyntaxError: invalid syntax[color=green][color=darkred]
    > >>>[/color][/color]
    >
    > is this prohibited for some reasons or is this just happens to be
    > disallowed?[/color]

    exec is a statement. And statements aren' allowed in the _expression_ of a
    list-comprehension.
    [color=blue]
    > this is one more cool way[color=green][color=darkred]
    > >>> p = "z%i = complex(1-1e-%i, 1-1e-%i);"
    > >>> c = reduce(lambda x,y: x+y, [p % (i,i,i) for i in range(20,30)])
    > >>> exec c[/color][/color]
    >
    > and one more :)[color=green][color=darkred]
    > >>> p = "z%i = complex(1-1e-%i, 1-1e-%i);"
    > >>> c = "".join([ p % (i,i,i) for i in range(20,30) ])
    > >>> exec c[/color][/color][/color]

    If you think so :) Ususally people go for dictionaries in such cases.

    Diez

    Comment

    • Schüle Daniel

      #3
      Re: [exec cmd for cmd in cmds]

      [...]
      [color=blue]
      > If you think so :) Ususally people go for dictionaries in such cases.[/color]

      you are right, I didn't think about dictionaries
      [color=blue][color=green][color=darkred]
      >>> p = "complex(1-1e-%i, 1-1e-%i)"
      >>> d={}
      >>> [d.update({i:eva l(p % (i,i))}) for i in range(20,30)][/color][/color][/color]
      [None, None, None, None, None, None, None, None, None, None]

      so now the work is complete :)

      Regards

      Comment

      • Scott David Daniels

        #4
        Re: [exec cmd for cmd in cmds]

        Schüle Daniel wrote:[color=blue]
        > you are right, I didn't think about dictionaries[color=green][color=darkred]
        > >>> p = "complex(1-1e-%i, 1-1e-%i)"
        > >>> d={}
        > >>> [d.update({i:eva l(p % (i,i))}) for i in range(20,30)][/color][/color]
        > [None, None, None, None, None, None, None, None, None, None]
        >
        > so now the work is complete :)
        >
        > Regards
        >[/color]
        Really, isn't this clearer?:

        d = {}
        for i in range(20, 30):
        v = 1. - 10. ** -i
        d[i] = complex(v, v)

        If you must repair the mess above, try:

        p = "complex(1-1e-%i, 1-1e-%i)"
        d = dict([(i, eval(p % (i, i))) for i in range(20, 30)])

        Strive to be clear first, terse second given the first is still
        achieved.

        --Scott David Daniels
        scott.daniels@a cm.org

        Comment

        Working...