2

I have a list which has the divisors of 10 like this:

print filter(lambda x: 10 % x == 0, range(1, 10))
>>>[1, 2, 5]

Then I need a List of function like that :

F =  [lambda x:k+x for k in filter(lambda x: 10 % x == 0, range(1, 10))]

When I call every functions in the List, it turns out to be wrong:

for f in F:
   print(f(0))
>>> 5
>>> 5
>>> 5

so How can I get the right list of function?

0

1 Answer 1

1

the lambda function captures the variable k. To get around it, you can copy it to a variable local to the lamba function:

F =  [lambda x, k=k:k+x for k in filter(lambda x: 10 % x == 0, range(1, 10))]
Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.