I am trying to create a list of functions that I then apply to a list of numbers. I do this by iterating through a range of numbers, and defining a lambdas function with each number from the for loop. Then a second for loop is used to apply each function to the same list of 10 numbers.
The problem is that my list of lambdas seem to all take the final value of the for loop.
fx_lst = []
for k in range(1,3,1):
func = lambda x_: k * x_
fx_lst.append(func)
xlst = range(1,10,1)
for fx in fx_lst:
ylst = map(lambda xin_: fx(xin_), xlst)
print i, ylst
ylst prints: [2,4,6...18] [2,4,6...18]
Obviously I'm not understanding something about the way lambdas store variable information.