sum()函数似乎将地图对象弄乱了 [英] The sum() function seems to be messing map objects

查看:51
本文介绍了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)

高度应该是一个地图对象,其中包含(或产生)两个现有高度条目的列表:[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:\Users\Hugo\Dropbox\Programmering\PythonProjekt\exercise2.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:\Users\Hugo\Dropbox\Programmering\PythonProjekt\exercise2.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:\Users\Hugo\Dropbox\Programmering\PythonProjekt\exercise2.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()函数似乎将地图对象弄乱了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆