5

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

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

1
  • 3
    Do you want to do f(*a)? Commented Dec 17, 2014 at 19:01

1 Answer 1

2

You want the splat unpack trick:

>>> a = np.array(['hello ', 'world', 'blah', 'blah', 'blah'])
>>> f = lambda *args: args[0] + args[1]
>>> f(*a)
'hello world'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.