gpt4 book ai didi

python - 如何解决 + : 'int' and 'tuple' because of trying to return 2 values with lambda? 不受支持的操作数类型

转载 作者:行者123 更新时间:2023-12-01 09:22:12 25 4
gpt4 key购买 nike

我尝试使用 lambda 返回 2 个值,但最终收到此错误:

TypeError: unsupported operand type(s) for +: 'int' and 'tuple'

这是我收到错误的代码:

def mean(n):
return sum(n)/len(n)

dataset = [
{"name": "John", "distance": 5602, "max-speed": 504},
{"name": "Mike", "distance": 5242, "max-speed": 622},
{"name": "Chad", "distance": 4825, "max-speed": 453},
{"name": "Phil", "distance": 611, "max-speed": 500},
{"name": "Tyler", "distance": 5436, "max-speed": 909}
]

mean(list(map(lambda x : (x["distance"], x["max-speed"]), dataset)))

如果只是 x["distance"] 或 x["max-speed"],它将返回我想要的值。我希望输出是

mean of distance

mean of max-speed

提前致谢。

最佳答案

是吗?

def mean(n):
return (sum([x for x,y in n]) / len(n),
sum([y for x,y in n]) / len(l))

输出:

(4343.2, 597.6)

关于python - 如何解决 + : 'int' and 'tuple' because of trying to return 2 values with lambda? 不受支持的操作数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50724680/

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