Python 3.6 how to make a regular function work same way as lambda -
i'm bit puzzled converting following lambda function regular function:
my_fun = lambda x,y:[i(j) i,j in zip(x,y)]
where x list of types, say
types = [float,float,float]
and y list of integer values, say:
values = [1,2,3]
so, my_fun() converts integers floats. when run way:
new_values = my_fun(types,values)
it returns new list floats.
now when comes define function in regular way:
def my_fun(x,y): i,j in zip(x,y): i(j)
it stops working. i'm not putting return inside / outside of loop return first / last instance once hits , not trying assign new list like:
def my_fun(x,y): new_vals = [] i,j in zip(x,y): new_vals.append(i(j)) return new_vals
because in case function looks overwhelmed me. can please explain fundamental difference between lambda , regular function in case, seems missing simple basic knowledge python 3.6?
i thought might have been list-comprehension i'm using in lambda couldn't explain myself. many thanks!
this seems more misunderstanding list comprehensions. express as:
def my_fun(x,y): return [i(j) i,j in zip(x,y)]
the problem in first example don't return anything. use yield give iterator.
def my_fun(x,y): i,j in zip(x,y): yield i(j)
Comments
Post a Comment