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?
Related
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?
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:
How to test the membership of multiple values in a list
(12 answers)
Closed 3 years ago.
I have an if statement which always returns as true even if it is not.
I have a global list :
NUMBERS_LIST = []
Numbers get added to it via an API call, that works fine.
When it does the following:
def func():
if 8 or 9 in NUMBER_LIST:
return true
elif 1 or 2 in NUMBER_LIST:
return true
But for some reason, it always returns true on the first if statement, even if NUMBER_LIST = [1]
I debugged my program and can see that NUMBER_LIST does contain 1, it's type is int.
I tried doing int(8), converting both types to str but that did not fix my issue.
When I debugged and step through the program, it doesn't really tell me much, I am using Pycharm.
or does not distribute. What you have written is not equivalent to
if 8 in NUMBER_LIST or 9 in NUMBER_LIST:
which is what you want, but to
if 8 or (9 in NUMBER_LIST):
Since 8 is a truthy value, the containment operation is never evaluated.
This question already has answers here:
Numpy array assignment with copy
(3 answers)
Closed 3 years ago.
I am wondering if someone can explain why Python modifies the original variable after assigning it to another variable and then passing the second variable the function call:
Consider the following example code:
Assume A is the original variable:
A=np.array(([1,20,30,40,10,5,60]))
B=A
B.sort()
print(A)
print(B)
The output for both is the same:
[ 1 5 10 20 30 40 60]
[ 1 5 10 20 30 40 60]
A is the original variable and I assigned it the B and then I sort B, then why both A and B are sorted ? what if I want to sort B only and compare it to A
if you say B=A where A is an Array, Python just makes a new Pointer to A
You can do
A = B[:]
to copy Array
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