Velocity Reviews > [exec cmd for cmd in cmds]

[exec cmd for cmd in cmds]

=?ISO-8859-1?Q?Sch=FCle_Daniel?=
Guest
Posts: n/a

 03-08-2006
Hello all,

>>> 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:

.... exec item
....
>>>
>>> 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]

File "<stdin>", line 1
[exec item for item in lst]
^
SyntaxError: invalid syntax
>>>

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

this is one more cool way
>>> 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

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

Regards, Daniel

Diez B. Roggisch
Guest
Posts: n/a

 03-08-2006
SchÃ¼le Daniel wrote:

> Hello all,
>
> >>> 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:

> ... exec item
> ...
> >>>
> >>> 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]

> File "<stdin>", line 1
> [exec item for item in lst]
> ^
> SyntaxError: invalid syntax
> >>>

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

exec is a statement. And statements aren' allowed in the _expression_ of a
list-comprehension.

> this is one more cool way
> >>> 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

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

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

Diez

=?UTF-8?B?U2Now7xsZSBEYW5pZWw=?=
Guest
Posts: n/a

 03-08-2006
[...]

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

you are right, I didn't think about dictionaries

>>> p = "complex(1-1e-%i, 1-1e-%i)"
>>> d={}
>>> [d.update({i:eval(p % (i,i))}) for i in range(20,30)]

[None, None, None, None, None, None, None, None, None, None]

so now the work is complete

Regards

Scott David Daniels
Guest
Posts: n/a

 03-08-2006
Schüle Daniel wrote:
> you are right, I didn't think about dictionaries
> >>> p = "complex(1-1e-%i, 1-1e-%i)"
> >>> d={}
> >>> [d.update({i:eval(p % (i,i))}) for i in range(20,30)]

> [None, None, None, None, None, None, None, None, None, None]
>
> so now the work is complete
>
> Regards
>

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
http://www.velocityreviews.com/forums/(E-Mail Removed)