gpt4 book ai didi

python - 类型错误 : () takes exactly 1 argument (3 given)

转载 作者:太空狗 更新时间:2023-10-30 00:40:06 25 4
gpt4 key购买 nike

给定这段代码:

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

我得到:

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)

当然,问题是,为什么?

最佳答案

您可以删除 lambda 参数周围的括号:

sumThree = lambda x, y, z: (x[0]+ y[0] + z[0], x[1] + y[1] + z[1])

或者以其他方式将参数作为单个元组传递给原始 lambda:

print 'sumThree((1, 2), (3, 4), (5, 6)) = {0}'.format(sumThree(((1, 2), (3, 4), (5, 6))))

关于python - 类型错误 : <lambda>() takes exactly 1 argument (3 given),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31447844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com