This question already has answers here:
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Python Argument Binders
(7 answers)
Closed 2 years ago.
The following Python program:
d = {}
for x in range(3):
d[x] = lambda: x
print(d[0](), d[1](), d[2]())
outputs:
2 2 2
x is bound by reference in the lambda expression. After the for statement, x is bound to 2, which explains the output.
I would like x to be bound by value instead, to get the following output:
0 1 2
How can I achieve this?
Related
This question already has answers here:
Local variables in nested functions
(4 answers)
Lambda in a loop [duplicate]
(4 answers)
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Closed 2 years ago.
I want to generate the Lagrange basis polynomials for a set of points where T is the set of points and n is the amount of given points. What I did is ci = [lambda t: np.prod([(t - T[j]) / (T[i] - T[j]) for j in range(n) if j != i] for i in range(n)], however, the resulting lambdas are all the same.
I checked by printing [c(val) for c in ci] in the console for several different values and it all returns the same value. How is this possible, when clearly the formula I put in should return lambdas with a different structure for each i?
This question already has answers here:
What's the best way to return multiple values from a function? [duplicate]
(6 answers)
Alternatives for returning multiple values from a Python function [closed]
(14 answers)
Closed 2 years ago.
So i have a function that has to return 3 values, i haven't found a better way to do this other than returning a list. Is this code a good programming practice? And if not how to fix it.
Example function:
def func():
#code
return [a,b,c]
Main code:
#code
list = func()
k = list[0]
l = list[1]
m = list[2]
You can pack/unpack directly in python:
def func():
a = 1
b = 2
c = 3
return a, b, c
k, l, m = func()
This question already has answers here:
What do lambda function closures capture?
(7 answers)
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Closed 3 years ago.
Let's say I want to create a list of functions programmatically, e.g. I want to create nine functions that, given a number in input, they add it respectively 1, 2, ... 9 and return it.
My take would be to write the following code:
>>> functions = []
>>> for i in range(1,10):
... functions.append(lambda x : x + i)
...
>>> for f in functions:
... print(f(0), end=' ')
However, the ouput is
9 9 9 9 9 9 9 9 9
Instead of the expected
1 2 3 4 5 6 7 8 9
I understand that this has to do with how Python deals with function arguments, i.e. when I create my lambda, I bound it to the reference of i, and not to its value.
How can I get around this? Is there a way to force Python to make a copy of i, instead of passing is reference?
This question already has answers here:
What do lambda function closures capture?
(7 answers)
Local variables in nested functions
(4 answers)
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Closed 4 years ago.
I found a interesting behavior of lambda function in Python.
If I do,
lambda_funcs = []
for i in range(10):
lambda_func = lambda x:x[i]
lambda_funcs.append(lambda_func)
X = ['a', 'b', 'c']
print lambda_funcs[0](X)
It throws an error: IndexError: list index out of range.
This code aims to create 10 lambda functions each of which returns i-th element. But, all lambda_func in the above code attempts to retrieve 10th element.
To make it work, I found that the following definition is a right way:
lambda_func = (lambda j: lambda x: x[j])(i)
Can anyone tell me what is going on inside lambda evaluation in Python?
This question already has answers here:
How does a for loop evaluate its argument
(3 answers)
Closed 6 years ago.
counter = Counter()
// fill data into counter
for a, b in counter.most_common():
if (b > 1):
counter[a] = np.log(b)
else:
counter[a] = -np.log((1 / (b+0.01)))
As I see, this is safe, based on my trial. No bad thing happens when I change the collection while I am enumerating it. In other languages, in each cycle of the for, the counter.most_common() value is evaluated.
Doesn't this happen in Python as well?
No, it doesn't. A more illustrative example:
def example():
print("Ping")
return [1,2,3,4]
for x in example():
print(x)
Output:
Ping
1
2
3
4