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

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() 函數似乎弄亂了地圖對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: www.天天操.com | 中文字幕亚洲欧美日韩在线不卡 | 免费观看av| 激情黄色在线观看 | 欧美午夜精品 | 欧美亚洲国语精品一区二区 | 久久久久国产精品免费免费搜索 | 日韩日韩日韩日韩日韩日韩日韩 | 玖玖视频免费 | a欧美 | 久久久网| 不卡视频一区 | 极情综合网 | 日本免费一区二区三区视频 | 欧美久久不卡 | 啪啪免费网 | 二区在线观看 | 欧美激情一区 | 午夜视频导航 | 亚洲精品久久久蜜桃网站 | 国产精品欧美精品 | a在线观看免费 | 久久99精品久久久久子伦 | 亚洲精品久久久久久久久久久久久 | 国产成人在线视频 | 99精品99久久久久久宅男 | 国产亚洲精品久久久久久牛牛 | 久久精品中文 | caoporn视频| 男人视频网站 | 中文字幕在线欧美 | 九九久久久 | 亚洲精品一区二三区不卡 | 国产视频欧美 | 99久久精品国产一区二区三区 | 黄色av网站在线免费观看 | 91精品久久久久久久久中文字幕 | 亚洲成人免费在线 | 99久久99| 国产一二区在线 | 亚洲女人天堂网 |