使用降低功能时出乎意料的typeError

发布于 2025-02-04 01:26:24 字数 165 浏览 4 评论 0原文

这是代码行:

product = list(reduce(lambda a, b: a*b, [2,2,2,2,2]))

错误:

typeError:'int'对象不是Itable

Here is the line of code:

product = list(reduce(lambda a, b: a*b, [2,2,2,2,2]))

Error:

TypeError: 'int' object is not iterable

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

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

发布评论

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

评论(2

橘和柠 2025-02-11 01:26:24

如果要将结果放入列表中,则可以使用此代码:

import functools
product = [functools.reduce((lambda x,y:x*y),[2,2,2,2,2])]
print(product)

无法使用列表函数,因为它与类型转换相对应,因为结果是整数。

if you want to put your result in a list, you can use this code:

import functools
product = [functools.reduce((lambda x,y:x*y),[2,2,2,2,2])]
print(product)

The list function cannot be used because it corresponds to a type conversion which is not possible as your result is an integer.

掌心的温暖 2025-02-11 01:26:24
>>> reduce(lambda a, b: a*b, [2,2,2,2,2])
32

因此,您无法制作列表

>>> reduce(lambda a, b: a*b, [2,2,2,2,2])
32

So you can't make a list of it.

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