python减少错误?

发布于 2024-08-11 21:34:59 字数 388 浏览 10 评论 0原文

以下是我的 python 代码:

>>> 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)
>>> 

我该如何修复它? 谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太傻旳人生 2024-08-18 21:34:59

您的 lambda 仅接受一个参数,但 reduce 需要一个接受两个参数的函数。让你的 lambda 接受两个参数。

由于您没有说出您希望这段代码做什么,我只是猜测:

the_sum=reduce(lambda x,y:abs(y[1]-x[1]),a)

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)
原谅我要高飞 2024-08-18 21:34:59

你的问题本身有点不清楚。不管怎样,我只是假设——

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

我假设你可能有兴趣获得列表中所有元素的总和。如果这是问题所在,那么可以分两步解决:

1)第一步应该是压平列表。

2)然后添加列表的所有元素。

>>> 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

一个衬垫

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

另一个假设,如果您的问题是逐项减去列表中元组的每个元素,则使用此:

>>> [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.

如果问题是其他问题,请详细说明。

PS:Python 列表理解比其他任何东西都更好、更高效。

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.

疑心病 2024-08-18 21:34:59

reduce 期望给定的函数接受 2 个参数。对于可迭代中的每个项目,它将向函数传递当前项目以及函数的前一个返回值。因此,获取列表的总和是 reduce(lambda: x,y: x+y, l, 0)

如果我理解正确,要获得您想要获得的行为,请更改代码to:

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

但我可能误解了你想要得到的东西。
更多信息位于 reduce 函数的文档字符串中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文