Using Iterator inside a lambda statement [duplicate] - python

This question already has answers here:
Lambda in a loop [duplicate]
(4 answers)
What do lambda function closures capture?
(7 answers)
Closed 2 years ago.
Why is the lambda expression only outputting the same result?
I expected to get the current value of the iterator and therefore the lambda expressions would output the numbers from 1 to 4 but that is obviously not the case.
I assume that the problem may have something to do with how the iterator is treated by the lambda expression but i could not find a answer to it yet. Thank you in advance
list = []
for i in range(5):
list.append(lambda:i)
for a in list:
print(a())
---OUTPUT---
4
4
4
4

Related

How to clone a list within itself? [duplicate]

This question already has answers here:
Repeating list in python N times? [duplicate]
(4 answers)
Closed 12 months ago.
suppose I have a list
a = [1,2,4,5]
I want to create another list b which clones a and adds it 3 times. So b is expected to be
b = [1,2,4,5,1,2,4,5,1,2,4,5]
How do I do this easily?
Methods using pandas or numpy is also welcome.
#just use the * operator
b=a*3

python "all" function making list empty [duplicate]

This question already has answers here:
Why can't I iterate twice over the same iterator? How can I "reset" the iterator or reuse the data?
(5 answers)
Closed 4 years ago.
I am new to python and was learning its builtins but function all()
is not behaving as expected i don't know why.Here is my code
n=map(int,input().strip().split())
print(all([j>0 for j in n]))
print(list(n)) #this line returning empty list
Here are my inputs:
1 2 3 4 5 -9
And my output:
False
Does function all changes the original map object(values)? but something like this is not mentioned in given function definition on the docs link.
Thanks in advance
Map returns a generator object which you exhausted in your all function. Therefore when you call list on n, since n is empty/exhausted, it returns an empty list.
To fix just make n a list in the first place.
n=list(map(int,input().strip().split()))
print(all([j>0 for j in n]))
print(n)

3rd argument of python range [duplicate]

This question already has answers here:
Python's `range` function with 3 parameters
(3 answers)
Closed 5 years ago.
I am trying to create a list of lists using a list comprehension method. I found this solution online:
output = [1,2,3,4,5,6]
[output[i:i+2] for i in range(0, len(output), 2)]
This is the first time I have come across a 3rd argument in range, What does the 3rd argument of range do?
The 3rd argument of range is the stepping, meaning how much to increment the last value for:
>>> for i in range (0,10,2):
... print i
0
2
4
6
8

Dynamic arguments for Python functions [duplicate]

This question already has answers here:
Expanding tuples into arguments
(5 answers)
Closed 7 years ago.
I have a list of lists, and I would like to enter them into a function in such a way that each element of the main list is a different argument to the function.
squares = [[1,5,9,13], [2,6,10,14], [3,7,11,15], [4,8,12,16]]
print zip(squares[0], squares[1], squares[2], squares[3])
# displays [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
Is there a way to do something like print zip(arguments(squares)) so that the arguments can be entered dynamically?
This is what the * operator is for:
print zip(*squares)

Different iterators are giving me different answers [duplicate]

This question already has answers here:
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
What do lambda function closures capture?
(7 answers)
Closed 9 years ago.
What is the difference between
for i in range(0,3): print cons[i]['fun'](x0)
and
for f in cons: print f['fun'](x0)
where cons is defined as following
A = np.asmatrix([[1,0,0],[0,1,0],[0,0,1]])
x0 = np.asarray([1,2,0])
cons = list()
for i in range(0,3): cons.append({'fun':lambda x: np.dot(A[i],x)})
Your problem probably is related to the fact that you are having a lambda clause using an unbound variable (i). Change your code like this:
for i in range(0,3): cons.append({'fun':lambda x, i=i: np.dot(A[i],x)})
(I. e. just insert that , i=i.)
This way the value of i is part of the lambda clause and not taken from the surrounding scope.

Categories