問題描述
我正在做這個代碼練習以嘗試在 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() 函數似乎弄亂了地圖對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!