Reputation: 3
How can I use the eval
statement with a list of strings?
Example: a list ["math.sin", "math.cos", "lambda x: x * 2", "lambda x: x ** 2"]
and a value x = 1
.
My task is to return a list with results of the expressions.
Upvotes: 0
Views: 80
Reputation: 198324
import math
exprs = ["math.sin", "math.cos", "lambda x: x * 2", "lambda x: x ** 2"]
x = 1
functs = map(eval, exprs)
# or: functs = (eval(e) for e in exprs)
results = [f(x) for f in functs]
Upvotes: 2