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

Python 3 與 Python 2 中的映射

map in Python 3 vs Python 2(Python 3 與 Python 2 中的映射)
本文介紹了Python 3 與 Python 2 中的映射的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我是 Python 新手,正在閱讀一本 Python 舊書.它基于 Python 2,所以有時(shí)我對細(xì)節(jié)有點(diǎn)困惑.

I'm a Python newbie reading an old Python book. It's based on Python 2, so sometimes I got little confused about detail.

有代碼

L=map(lambda x:2**x, range(7))

所以它不會返回 python 3 中的列表,我用谷歌搜索了它,發(fā)現(xiàn) list(L) 有效.但問題是,首先 list(L) 工作正常,但是當(dāng)我再次使用它時(shí),

so it doesn't return the list in python 3, and I googled it and found that list(L) works. But the problem is, first list(L) works fine, but when I use it again,

list(L)

list(L)

第二個(gè)返回[]

誰能解釋一下發(fā)生了什么?

Can somebody explain me what's happening?

推薦答案

map 返回一個(gè)迭代器.因此,它的輸出只能使用一次.如果您希望將結(jié)果存儲在列表中,與 Python 2.x 一樣,只需在使用 map 時(shí)調(diào)用 list:

map returns an iterator. As such, its output may only be used once. If you wish to store your results in a list, in the same way as Python 2.x, simply call list when you use map:

L = list(map(lambda x:2**x, range(7)))

列表 L 現(xiàn)在將包含您的結(jié)果,無論您調(diào)用它多少次.

The list L will now contain your results however many times you call it.

您面臨的問題是,一旦 map 迭代了一次,它將不會為每個(gè)后續(xù)調(diào)用產(chǎn)生任何結(jié)果.因此,您會看到第二次調(diào)用的空列表.

The problem you are facing is that once map has iterated once, it will yield nothing for each subsequent call. Hence you see an empty list for the second call.

如果您無法用盡迭代器但希望使用兩次,有關(guān)解決方法的更詳細(xì)說明和建議,請參閱 為什么我不能在同一個(gè)數(shù)據(jù)上迭代兩次.

For a more detailed explanation and advice on workarounds if you cannot exhaust your iterator but wish to use it twice, see Why can't I iterate twice over the same data.

這篇關(guān)于Python 3 與 Python 2 中的映射的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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ū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 午夜影院在线 | 九九热这里有精品 | 亚洲欧美日韩国产 | 国产www视频| 国产精品不卡 | 婷婷久 | 色羞羞 | 欧美一区二区视频在线观看 | 天天操夜夜撸 | 黄色网在线 | 欧美日韩一区二 | 色天天综合 | 一区二区免费 | 久久久久久国产 | 美女视频福利 | 亚洲成人毛片 | 午夜在线 | 亚洲精品二区 | 欧美国产日韩在线 | www国产亚洲精品久久网站 | 久久久亚洲精品视频 | 日韩特级片 | www.欧美精品 | 午夜影院在线免费观看 | 国产性猛交96 | 天天草天天射 | 日韩在线一区二区 | 国内精品视频 | 性史性dvd影片农村毛片 | 亚洲高清在线观看 | 亚洲欧美视频 | 99国产精品99久久久久久粉嫩 | 国产成人精品亚洲男人的天堂 | 日本不卡在线 | 国产精品视频一区二区三区 | 少妇一级片| 成人av一区二区三区在线观看 | 日韩a在线观看 | 亚洲精品视频在线播放 | av网址在线播放 | 日韩av在线影院 |