Python using a string (with variables) in lambda expression
Question: I want to use a string of a polynomial in a lambda expression, but I couldn't
figure out how to do it. It may not be possible, but if it is I'd love some
help!

An example of what I'm trying to do is:

    
    
    f = lambda x: '3*x^3 + 2*x^2 -4*x +8'
    

Assume that I only have access to the polynomial in type str.

Is there something that removes the quotes or a version of eval that works
with variables?


Answer: Instead of a `lambda` you can use
[np.polyval](http://docs.scipy.org/doc/numpy/reference/generated/numpy.polyval.html)
from `numpy`

    
    
    In [19]: import numpy as np
    
    In [20]: np.polyval([3,2,-4,8],2)
    Out[20]: 32
    

If you meant `4^x` then a quick fix

    
    
    In [21]: np.polyval([3,2,0,-pow(4,2)+8],2)
    Out[21]: 24
    

or with lambda

    
    
    f=lambda mylist,x: sum((x**power) * coeff for power, coeff in enumerate(reversed(mylist)))
    
    print f([3,2,-4,8],2)
    

