Different iterators are giving me different answers [duplicate] - python

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.

Related

Using Iterator inside a lambda statement [duplicate]

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

Single line for loop [duplicate]

This question already has answers here:
What does "list comprehension" and similar mean? How does it work and how can I use it?
(5 answers)
Closed 2 years ago.
Can someone help me rewrite this single line loop to a multiple lines for loop in python?
I am trying to understand how it is formatted.
y = {element: r for element in variables(e)}
The dictionary comprehension is equivalent to this loop:
y = {}
for element in variables(e):
y[element] = r
This is called a dict comprehension in python.
This syntax builds a dictionary by taking each element in the list variables(e) and associating it with the value r.

Lambda Function Call [duplicate]

This question already has answers here:
What do lambda function closures capture?
(7 answers)
Closed 3 years ago.
The following is a code snippet which I don't seem to get. The question is how to make the function output the desired result (not mentioning what the desired result is, I assume its printing 0 to 9).
Here is the question:
What does the below code snippet print out? How can we fix the anonymous functions to behave as we'd expect?
functions = []
for i in range(10):
functions.append(lambda : i)
for f in functions:
print(f())
In Python, no new scope will be produced in for loop
So after for i in range(10), the variable i is still exist, and its value == 9. And the lambda function lambda : i access the variable i
In order to output your desired result, you should pass the variable as a function argument in loop
functions = []
for i in range(10):
functions.append(lambda i=i: i)
for f in functions:
print(f())

Python calling function by string name from code [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 7 years ago.
Here is what I would like to be able to do:
I have a file called functions, with lots of functions. The functions are all essentially the same, functionally speaking (i.e., they are all of the form: pandas.Dataframe -> pandas.Dataframe). Obviously, they do different things to the Dataframe, so in that sense they are different.
I'd like to be able to pass my main function a list of strings, which would be the actual function names in the module, and have my program translate the strings into function calls.
So, basically, instead of:
functions = [module.functionA, module.functionB, module.functionC]
x = g(functions)
print(x)
> 'magical happiness'
I would have:
function_strings = ['functionA','functionB','functionC']
functions = interpret_strings_as_function_calls(module,function_strings)
x = g(functions)
print(x)
> 'magical happiness'
Is there a way to do this? Or do I need to write a function in the module that matches each string with it's corresponding function? i.e.:
def interpret_strings(function_string):
if function_string == 'functionA':
return module.functionA
elif function_string == 'functionB':
return module.functionB
etc.
(or in a switch statement, or whatever)
You can use getattr(module, function_string).

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)

Categories