4

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!

abyx
  • 69,862
  • 18
  • 95
  • 117
Charlie Epps
  • 3,595
  • 9
  • 33
  • 38
  • 1
    Please note that `sum` is a built-in function in python, and so it is recommended that you use a different name, like I did in my answer. – abyx Nov 17 '09 at 06:59
  • I have change the sum to sums, and the same error occures. – Charlie Epps Nov 17 '09 at 07:14
  • Yes, of course. The answers below explain what the real problem is. I just said it is a better practice to now shadow the `sum` function :) – abyx Nov 17 '09 at 07:23
  • Furthermore, I think you can see from the answers that it is not clear what it is that you're trying to do. Can you add a description of what that code is supposed to accomplish? – abyx Nov 17 '09 at 09:31

3 Answers3

8

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)
S.Lott
  • 384,516
  • 81
  • 508
  • 779
Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
3

Your problem itself is a bit unclear. Anyway, i have taken just assumption--

>>> a = []
>>> a.append((1,2,3))
>>> a.append((7,2,4))
>>> a
[(1, 2, 3), (7, 2, 4)] # list of tuples

I am assuming that you might be interested in getting the sum of all the elements in the list. If that is the problem then that could be solved in 2 steps

1) The first step should be to flatten the list.

2) And then add all the elements of the list.

>>> new_list = [y for x in a for y in x] # List comprehension used to flatten the list
[1, 2, 3, 7, 2, 4] 
>>> sum(new_list)
19

One liner

>>> sum([y for x in a for y in x])
19

Another assumption, if your problem is to minus every element of tuple by item in the list then use this:

>>> [tuple(map(lambda y: abs(item - y), x)) for x in a]
[(0, 1, 2), (6, 1, 3)] # map function always returns a list so i have used tuple function to convert it into tuple.

If the problem is something else then please elaborate.

PS: Python List comprehension is far better and efficient than anything else.

aatifh
  • 2,317
  • 4
  • 27
  • 30
2

reduce expects the function it is given to accept 2 arguments. For every item in the iterable it will pass the function the current item, and the previous return value from the function. So, getting the sum of a list is reduce(lambda: x,y: x+y, l, 0)

If I understand correctly, to get the behavior you were trying to get, change the code to:

a_sum = reduce(lambda x,y: x + abs(item-y[1]), a, 0)

But I might be mistaken as to what you were trying to get. Further information is in the reduce function's docstring.

abyx
  • 69,862
  • 18
  • 95
  • 117