sum() 函数似乎弄乱了地图对象

The sum() function seems to be messing map objects(sum() 函数似乎弄乱了地图对象)
本文介绍了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)

另一种方法.

您可以使用统计模块直接从迭代器计算算术平均值(和其他统计数据).查看文档.

这篇关于sum() 函数似乎弄乱了地图对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)