Lambda in a Loop

Lambda in a loop

You need to bind d for each function created. One way to do that is to pass it as a parameter with a default value:

lambda d=d: self.root.change_directory(d)

Now the d inside the function uses the parameter, even though it has the same name, and the default value for that is evaluated when the function is created. To help you see this:

lambda bound_d=d: self.root.change_directory(bound_d)

Remember how default values work, such as for mutable objects like lists and dicts, because you are binding an object.

This idiom of parameters with default values is common enough, but may fail if you introspect function parameters and determine what to do based on their presence. You can avoid the parameter with another closure:

(lambda d=d: lambda: self.root.change_directory(d))()
# or
(lambda d: lambda: self.root.change_directory(d))(d)

loop for inside lambda

Since a for loop is a statement (as is print, in Python 2.x), you cannot include it in a lambda expression. Instead, you need to use the write method on sys.stdout along with the join method.

x = lambda x: sys.stdout.write("\n".join(x) + "\n")

Creating lambda inside a loop

Use this line instead:

lambdas_list.append(lambda obj=obj: obj.some_var)

Using a for loop with lambda in filter method?

Your lambda expression returns a generator expression. You want it to test if the condition is True for all iterations. You need all.

Besides (more a mathematical issue), no need to loop to x, just use square root (inclusive) to reduce complexity.

My proposal:

print(max(filter(lambda x: all(x%i != 0 for i in range(2,int(x**0.5)+1)), a)))

How to apply lambda function in for loop

as others comment, you are printing the variable i and not calling at all the lambda

try instead:

x = lambda i: i.upper()
print(x(i))

Python lambda function is not being called correctly from within a for loop

This is a classic case of unwanted closure. Here's a simplified example:

funcs = []
for i in range(3):
funcs.append(lambda: i + 1)
for func in funcs:
print(func())

One might expect that this will print 1 2 3, but it prints 3 3 3 instead. The reason is, lambda is a closure, closing over the variable i (capturing it in its context). When we execute the functions, the variable i is left at its last value in the loop (2). To reiterate, the lambda does not capture the value, but the variable. To avoid this, we want to pass the current value of i into the function as a parameter. To do that, we can construct a function that accepts i as the parameter, captures the parameter into the closure and returns the "customised" function we want:

from functools import partial
funcs = []
for i in range(3):
funcs.append((lambda val: lambda: val + 1)(i))
for func in funcs:
print(func())

Equivalently, we can use functools.partial, which does just this:

from functools import partial
funcs = []
for i in range(3):
funcs.append(partial(lambda val: val + 1, i))
for func in funcs:
print(func())

Here, lambda val: val + 1 will expect a parameter; partial(lambda val: val + 1, 0) will produce a new function where the first parameter is fixed at 0 - basically, lambda: 0 + 1. This captures no variables, and thus avoids the problem you encountered.

tl;dr:

command=partial(lambda i: self.process(charOrder[i]), imgIndex)

Lambda function with for loop and if-else statement

Use corpus = set(corpus).

Then you can use something like

df['clean_text'].map(lambda l: "Relevant" if any(x in corpus for x in l) else "Irrelevant")

Note, the fact that you are using a lambda is really not relevant. You could have done something like:

def search_corpus(tokens):
if any(token in corpus for token in tokens):
return "Relevant"
return "Irrelevant"

And do:

df['clean_text'].map(search_corpus)

And this won't affect performance. lambda expressions don't create anything special, and you never have to use one.

How to write a Python for loop as a lambda function with apply or map instead?

A loop is good enough for this task, but if for some reason you must use lambda and map, you can do it like this:

list(map(lambda x: print(f"I only have {x} friends, but they are awesome."), range(2, 5)))

The expression needs to be wrapped in list() as the map function is eagerly executed, which means it doesn't calculate the output until it is needed.



Related Topics



Leave a reply



Submit