Call lambda functions iterating over an integer array of the form `(n,)`

Let it define a function f such that f = lambda x1,x2, ... , xn: x1 + x2 and a numpy array a = np.array([a1,a2, ... , an]) length n . How can I apply f using the components of a as an argument by iterating over each of its elements?

Note that I don't want to use f like this: y = f(a[0], .... a[n]) , but using a for loop (something like this: y = lambda(u for u in a) ).

+5
source share
1 answer

Do you want splat to unpack the trick:

 >>> a = np.array(['hello ', 'world', 'blah', 'blah', 'blah']) >>> f = lambda *args: args[0] + args[1] >>> f(*a) 'hello world' 
+2
source

Source: https://habr.com/ru/post/1209393/


All Articles