sum() 函数似乎弄乱了地图对象
问题说明
我正在做这个代码练习以尝试在 python 中进行函数式编程,但我遇到了 sum()、list() 和 map 对象的问题.我不明白我做错了什么,但 list() 函数似乎与我的地图对象搞砸了.
I'm doing this code excercise for trying out functional programming in python, and I ran into problems with sum(), list(), and map objects. I don't understand what I'm doing wrong, but the list() function seems to be screwing with my map object.
这是我的代码:
people = [{'name': 'Mary', 'height': 160},
{'name': 'Isla', 'height': 80},
{'name': 'Sam'}]
heights = map(lambda x: x['height'], filter(lambda x: 'height' in x, people))
print(len(list(heights)))
print(sum(list(heights)))
print(len(list(heights)))
average_height = sum(list(heights)) / len(list(heights))
print(average_height)
heights 应该是一个地图对象,包含(或产生)两个现有高度条目的列表:[160, 80].
heights should be a map object containing (or resulting in) a list of the two existing height entries: [160, 80].
打印长度应该是2,两者的和显然应该是240,平均应该是120.
printing the length should give 2, the sum of the two should obviously be 240, and the average should be 120.
不过,我面临的问题是我收到以下错误消息:
The problem I'm facing, though, is that I get this error message:
2
0
0
Traceback (most recent call last):
File "C:UsersHugoDropboxProgrammeringPythonProjektexercise2.py", line 12, in <module>
average_height = sum(list(heights)) / len(list(heights))
ZeroDivisionError: division by zero
是的,长度是对的,但是总和是0,第二个长度打印也是0.整个零除错误必须来自那里的某些东西,并且似乎是 list() 函数导致了它.更改打印顺序仍然只能让第一个打印语句正确:
Yes, the length is right, but the sum is 0, and the second length print is 0 aswell. The whole zero-division fault must come from something there, and it seems that the list() function is causing it. Changing the print order still only lets through the first print statement correctly:
print(sum(list(heights)))
print(len(list(heights)))
print(len(list(heights)))
给予:
240
0
0
Traceback (most recent call last):
File "C:UsersHugoDropboxProgrammeringPythonProjektexercise2.py", line 12, in <module>
average_height = sum(list(heights)) / len(list(heights))
ZeroDivisionError: division by zero
并删除 list() 函数:
and removing the list() function:
print(sum(list(heights)))
print(len(heights))
print(len(list(heights)))
给我:
240
Traceback (most recent call last):
File "C:UsersHugoDropboxProgrammeringPythonProjektexercise2.py", line 9, in <module>
print(len(heights))
TypeError: object of type 'map' has no len()
所以我不知道发生了什么.list() 函数不应该以任何方式更改地图对象,对吧?它仍然是一个地图对象,但不止一次调用 list() 似乎会改变它的行为.我很困惑.
So I have no idea what's going on. The list() function should not be changing the map object in any way, right? And it stays a map object, but calling list() on it more than once seems to change its behaviour. I am confuse.
正确答案
实际上,调用 list
确实会改变你的 map
对象.map
是一个迭代器,而不是一个数据结构(在 Python3 中).在它被循环一次之后,它就被耗尽了.要重现结果,您需要重新创建地图对象.
Actually, calling list
does change your map
object. map
is an iterator, not a data structure (in Python3). After it has been looped over once, it is exhausted. To reproduce the result, you need to recreate the map object.
在您的情况下,您可能想要做的是从地图创建一个列表来存储结果,然后执行 sum
.
In your case, what you probably want to do is create a list from the map to store the result, then perform the sum
.
heights = list(heights)
average = sum(heights) / len(heights)
另一种方法.
您可以使用统计模块直接从迭代器计算算术平均值(和其他统计数据).查看文档.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tangiabga
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20 -
希伯来语的空格句子标记化错误
it1352 06-22