UpmostScarab UpmostScarab - 5 months ago 10
Python Question

Sum of n lambda functions

I have a list of lambda functions. Lets say this one

l = [lambda x:x**i for i in range(n)]


For every n I need to be able to sum them so I'd have a function like this:

f = lambda x: x + x**2 + x**3 + ... + x**n


Is there any way?

Edit: I wasn't clear. I don't know anything about that functions.

Answer

Is this the solution you're looking for?

Python 3.x:

n = 5
g = lambda y: sum(  f(y) for f in (lambda x: x**i for i in range(n))  )
print(g(5)) # 781

Python 2.x:

n = 5
g = lambda y: sum(  f(y) for f in (lambda x: x**i for i in xrange(n))  )
print g(5) # 781
Comments