This question already has answers here:
What do lambda function closures capture?
(7 answers)
Closed 9 years ago.
Can anyone do sanity check?
I'm trying to make functions in for-loop.
The point I can't understand is summarized in the following code:
f_list = []
for i in range(10):
f = lambda j : i
f_list.append(f)
Then,
>>> f_list[0](0)
9 #I hope this is 0.
>>> f_list[1](0)
9 #I hope this is 1.
Why is this happen??
Edit: Almost the same problem is already discussed in Stackoverflow, here.
This is because of the closure property of python. To get what you actually need, you need to do like this
f = lambda j, i = i : i
So, the output of this program becomes like this
f_list = []
for i in range(5):
f = lambda j, i = i : i
f_list.append(f)
for i in range(5):
print f_list[i](0)
Output
0
1
2
3
4
Related
This question already has answers here:
How to change for-loop iterator variable in the loop in Python?
(6 answers)
Updating Index Value in For Loop
(2 answers)
Closed 1 year ago.
I have a similar code that needs incrementing and while loop cant be used there,
m = range(10)
for i in range(len(m)):
print(i)
i+=2
Do it like this:
m = range(10)
nb = 0
for i in range(len(m)):
print(nb)
nb+=2
The problem is that you wanted to use i for two different tasks.
This question already has answers here:
Python list comprehension expensive
(1 answer)
What is the advantage of a list comprehension over a for loop?
(1 answer)
Why is local variable access faster than class member access in Python?
(2 answers)
Closed 5 years ago.
I have tested four situations.
1.
testList = list()
for i in range(1000000):
testList.append(i)
2.
testList = list()
for i in range(1000000): testList.append(i)
3.
testList = list()
newAppend = testList.append
for i in range(1000000): newAppend(i)
4.
[i for i in range(1000000)]
Each time was
1 : 0.09166
2 : 0.08299
3 : 0.05003
4 : 0.04594
Why others are faster than 1?
Can I find related keywords?
This question already has answers here:
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Closed 5 years ago.
I tried to achieve something like this
list1 = [lambda x: x+0, lambda x: x+1]
by doing this
list2 = [lambda x: x+n for n in range(2)]
But it doesn't work! (Why not?)
How do I create a list of functions programmatically?
n is reassigned by the for expression, so after the for expression is completed both will see value of n being 1.
More typical example of this problem can be visible without list expression:
list3 = []
for n in range(2):
def f(x):
return x + n
list3.append(f)
Here also all created function objects will refer to the same variable n and it will be assigned value 1 and both function objects will work as lambda x: x + 1.
You can deal with it by creating additional scope with a variable for each created function:
a = [(lambda n: lambda x: x+n)(n) for n in range(2)]
This question already has answers here:
How are lambdas useful? [closed]
(26 answers)
Closed 6 years ago.
Help me understand Lambda functions, I checked a lot of codes but I still can't manage to understand it. Maybe with a simple example, thanks in advance.
Suppose you want to square in value in a list , foo = [1,2,3,4,5]
for i in range(len(a)):
a[i] = a[i] * a[i]
You could use lambda and write
map(lambda x: x * x, foo)
if you want only elements divisible by 3, then
filter(lambda x: x % 3 == 0, foo)
Basically it save you from writing a for loop or to put it better write it in an efficient way.
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 6 years ago.
I just want to declare some range of lists depending on the input.
For example, if I give the input as 4, I would be expecting 4 lists like
list1 = []
list2 = []
list3 = []
list4 = []
I have tried this
for i in range (1,4):
list(%d) %i= []
But its not working.
I agree with PM-2ring's that in most contexts a list of lists or a dict would be more appropriate.
d = {}
for i in range(1,4):
d['list%s' % i] = []
or
l = [[] for x in range(1,4)]
But to answer the question actually asked in case this is one of the very rare cases where it is the right thing to do even with all the problems associated with dynamic creation of variables (maintainability, access, sanitizing user input, etc).
You probably shouldn't do any of the below if it is new information to you.
It depends on the context a bit. If you're inserting them into an object it would be:
a = classA()
for i in range(1,4):
setattr(a, 'list%s' % i, [])
If trying to create a global var:
for i in range(1,4):
globals()['list%s' % i] = []