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

Python sum()函数似乎弄乱了贴图对象,python,python-3.x,dictionary,sum,Python,Python 3.x,Dictionary,Sum,我正在做这个代码练习,以试用python中的函数式编程,但在sum()、list()和map对象方面遇到了问题。我不明白我做错了什么,但是list()函数似乎在处理我的map对象 这是我的代码: people = [{'name': 'Mary', 'height': 160}, {'name': 'Isla', 'height': 80}, {'name': 'Sam'}] heights = map(lambda x: x['height'], fi

我正在做这个代码练习,以试用python中的函数式编程,但在sum()、list()和map对象方面遇到了问题。我不明白我做错了什么,但是list()函数似乎在处理我的map对象

这是我的代码:

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]

打印长度应为2,两者之和显然应为240,平均值应为120

不过,我面临的问题是,我收到了以下错误消息:

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
给出:

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
给我:

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()
240
回溯(最近一次呼叫最后一次):
文件“C:\Users\Hugo\Dropbox\Programmering\PythonProjekt\exercise2.py”,第9行,在
印刷品(透镜(高度))
TypeError:类型为“map”的对象没有len()

所以我不知道发生了什么。list()函数不应该以任何方式更改映射对象,对吗?它仍然是一个映射对象,但多次调用它的list()似乎会改变它的行为。我很困惑。

实际上,调用
list
确实会更改您的
map
对象<代码>映射是迭代器,而不是数据结构(在Python3中)。循环一次后,它就耗尽了。要再现结果,需要重新创建贴图对象

在您的情况下,您可能要做的是从映射创建一个列表来存储结果,然后执行
sum

heights = list(heights)
average = sum(heights) / len(heights)
编辑:另一种方法。
您可以使用统计模块直接从迭代器计算算术平均值(和其他统计数据)。签出。

实际上,调用
list
会更改您的
map
对象<代码>映射是迭代器,而不是数据结构(在Python3中)。循环一次后,它就耗尽了。要再现结果,需要重新创建贴图对象

在您的情况下,您可能要做的是从映射创建一个列表来存储结果,然后执行
sum

heights = list(heights)
average = sum(heights) / len(heights)
编辑:另一种方法。
您可以使用统计模块直接从迭代器计算算术平均值(和其他统计数据)。签出。

您可以通过在
映射
结构上迭代来创建列表,以避免每次列表调用都耗尽迭代器:

people = [{'name': 'Mary', 'height': 160},
      {'name': 'Isla', 'height': 80},
      {'name': 'Sam'}]

heights = [i for i in map(lambda x: x['height'], filter(lambda x: 'height' in x, people))]
average_height = sum(heights)/len(heights)
输出:

120.0

您可以通过在
map
结构上迭代来创建列表,以避免每次列表调用都耗尽迭代器:

people = [{'name': 'Mary', 'height': 160},
      {'name': 'Isla', 'height': 80},
      {'name': 'Sam'}]

heights = [i for i in map(lambda x: x['height'], filter(lambda x: 'height' in x, people))]
average_height = sum(heights)/len(heights)
输出:

120.0

我想在这个答案中加入“发电机”的概念。这里有一些值得学习的东西。好主意@ziyad,理解列表、迭代器和生成器之间的区别是python的基本知识!当我运行问题中给出的代码时,heights确实给了我一个[160,80]的列表,这是为什么?为什么代码对我来说运行得很好?我正在使用Python2.7。@Python2.7
map
中的FatihAkici将返回一个列表。请参见@juanpa.arrivillaga是的,但是这个想法非常密切相关,如果有人阅读这个主题,介绍发电机的想法会很有用。我想在这个答案中添加“发电机”的想法。这里有一些值得学习的东西。好主意@ziyad,理解列表、迭代器和生成器之间的区别是python的基本知识!当我运行问题中给出的代码时,heights确实给了我一个[160,80]的列表,这是为什么?为什么代码对我来说运行得很好?我正在使用Python2.7。@Python2.7
map
中的FatihAkici将返回一个列表。请参阅@juanpa.arrivillaga是的,但是这个想法非常密切相关,如果有人阅读这个主题,介绍发电机的想法会很有用。你想得到什么?平均高度?您耗尽了
映射
迭代器,但从未保存结果…您试图获得的结果可能重复?平均高度?您耗尽了
映射
迭代器,但从未保存结果…可能重复