TypeError: <lambda> () takes exactly 1 argument (3)
Given this code:
sumThree = lambda (x, y, z): (x[0]+ y[0] + z[0], x[1] + y[1] + z[1])
print 'sumThree((1, 2), (3, 4), (5, 6)) = {0}'.format(sumThree((1, 2), (3, 4), (5, 6)))
I get:
TypeError Traceback (most recent call last)
<ipython-input-43-7f1b9571e230> in <module>()
16 # second position summed. E.g. (1, 2), (3, 4), (5, 6) => (1 + 3 + 5, 2 + 4 + 6) => (9, 12)
17 sumThree = lambda (x0, x1, x2): (x[0]+ y[0] + z[0], x[1] + y[1] + z[1])
---> 18 print 'sumThree((1, 2), (3, 4), (5, 6)) = {0}'.format(sumThree((1, 2), (3, 4), (5, 6)))
TypeError: <lambda>() takes exactly 1 argument (3 given)
And the question, of course, is why?
+4
2 answers
You made a lambda function that takes one argument: a tuple (x, y, z). If you call your function with a tuple as the only argument, it will work:
sumThree(((1, 2), (3, 4), (5, 6)))
Alternatively, you can re-write the lambda function so as not to accept one argument of the tuple, but three arguments:
sumThree = lambda (x, y, z): (x[0]+ y[0] + z[0], x[1] + y[1] + z[1])
, (.. def sumThree(x, y, z):). - (sumThree) , lambdas : .
+1