久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

sum() 函數(shù)似乎弄亂了地圖對象

The sum() function seems to be messing map objects(sum() 函數(shù)似乎弄亂了地圖對象)
本文介紹了sum() 函數(shù)似乎弄亂了地圖對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在做這個代碼練習(xí)以嘗試在 python 中進(jìn)行函數(shù)式編程,但我遇到了 sum()、list() 和 map 對象的問題.我不明白我做錯了什么,但 list() 函數(shù)似乎與我的地圖對象搞砸了.

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 應(yīng)該是一個地圖對象,包含(或產(chǎn)生)兩個現(xiàn)有高度條目的列表:[160, 80].

heights should be a map object containing (or resulting in) a list of the two existing height entries: [160, 80].

打印長度應(yīng)該是2,兩者的和顯然應(yīng)該是240,平均應(yīng)該是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() 函數(shù)導(dǎo)致了它.更改打印順序仍然只能讓第一個打印語句正確:

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() 函數(shù):

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()

所以我不知道發(fā)生了什么.list() 函數(shù)不應(yīng)該以任何方式更改地圖對象,對吧?它仍然是一個地圖對象,但不止一次調(diào)用 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.

推薦答案

實(shí)際上,調(diào)用 list 確實(shí)會改變你的 map 對象.map 是一個迭代器,而不是一個數(shù)據(jù)結(jié)構(gòu)(在 Python3 中).在它被循環(huán)一次之后,它就被耗盡了.要重現(xiàn)結(jié)果,您需要重新創(chuàng)建地圖對象.

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.

在您的情況下,您可能想要做的是從地圖創(chuàng)建一個列表來存儲結(jié)果,然后執(zhí)行 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)

另一種方法.

您可以使用統(tǒng)計(jì)模塊直接從迭代器計(jì)算算術(shù)平均值(和其他統(tǒng)計(jì)數(shù)據(jù)).查看文檔.

這篇關(guān)于sum() 函數(shù)似乎弄亂了地圖對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 日本一区二区三区四区 | 成人av在线播放 | 国产精久久久久久久妇剪断 | 成人精品鲁一区一区二区 | 日韩一区二区三区在线 | 亚洲精选一区二区 | 欧美a区| 国产在线观看网站 | 免费看片在线播放 | 一级毛片免费 | 国产精品 欧美精品 | 久久久久国产一区二区三区四区 | 日韩在线 | 一区二区在线不卡 | 国产精品亚洲精品日韩已方 | 成人精品国产免费网站 | 中文字幕精品一区二区三区在线 | 久久一 | 不卡视频一区二区三区 | 嫩草懂你的影院入口 | 久久久久久久久99精品 | 国产精品一区二区欧美 | 日韩精品一区二区三区视频播放 | 欧美综合一区二区 | 少妇一级淫片免费放播放 | 久久国产精品免费视频 | 精品欧美二区 | 婷婷亚洲综合 | 国产精品一级在线观看 | 欧美男人天堂 | 国产精品久久久久久久久久久久冷 | 一区二区av | 日韩在线一区二区 | 日日噜噜夜夜爽爽狠狠 | 91九色在线观看 | 日韩精品一区二区三区第95 | 亚洲免费在线 | 青春草91| 国产精品久久久久aaaa九色 | 欧美日韩视频在线播放 | 色视频在线播放 |