Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python reduce error?

The following is my python code:

>>> item = 1
>>> a = []
>>> a.append((1,2,3))
>>> a.append((7,2,4))
>>> sums=reduce(lambda x:abs(item-x[1]),a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes exactly 1 argument (2 given)
>>> 

How can I fix it? Thanks!

like image 905
Charlie Epps Avatar asked Dec 14 '22 01:12

Charlie Epps


1 Answers

Your lambda takes only one argument, but reduce requires a function that takes two arguments. Make your lambda take two arguments.

Since you didn't say what you want this code to do, I'll just guess:

the_sum=reduce(lambda x,y:abs(y[1]-x[1]),a)
like image 73
Laurence Gonsalves Avatar answered Dec 28 '22 10:12

Laurence Gonsalves