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

Python range() 和 zip() 對象類型

Python range() and zip() object type(Python range() 和 zip() 對象類型)
本文介紹了Python range() 和 zip() 對象類型的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我了解如何在 for 循環中使用 range()zip() 等函數.但是我希望 range() 輸出一個列表 - 很像 unix shell 中的 seq .如果我運行以下代碼:

I understand how functions like range() and zip() can be used in a for loop. However I expected range() to output a list - much like seq in the unix shell. If I run the following code:

a=range(10)
print(a)

輸出是range(10),表明它不是一個列表,而是一種不同類型的對象.zip() 在打印時有類似的行為,輸出類似

The output is range(10), suggesting it's not a list but a different type of object. zip() has a similar behaviour when printed, outputting something like

<zip object at "hexadecimal number">

所以我的問題是它們是什么,制作它們有什么優勢,以及如何在不循環它們的情況下將它們的輸出輸出到列表?

So my question is what are they, what advantages are there to making them this, and how can I get their output to lists without looping over them?

推薦答案

你必須使用 Python 3.

You must be using Python 3.

在 Python 2 中,對象 ziprange 確實按照您的建議行事,返回列表.它們已更改為類似 generator 的對象,這些對象按需生成元素,而不是將整個列表擴展為記憶.一個優勢是在它們的典型用例中效率更高(例如迭代它們).

In Python 2, the objects zip and range did behave as you were suggesting, returning lists. They were changed to generator-like objects which produce the elements on demand rather than expand an entire list into memory. One advantage was greater efficiency in their typical use-cases (e.g. iterating over them).

惰性"版本也存在于 Python 2.x 中,但它們有不同的名稱,即 xrangeitertools.izip.

The "lazy" versions also exist in Python 2.x, but they have different names i.e. xrange and itertools.izip.

要一次將所有輸出檢索到熟悉的列表對象中,您可以簡單地調用 list 來迭代并使用內容:

To retrieve all the output at once into a familiar list object, you may simply call list to iterate and consume the contents:

>>> list(range(3))
[0, 1, 2]
>>> list(zip(range(3), 'abc'))
[(0, 'a'), (1, 'b'), (2, 'c')]

這篇關于Python range() 和 zip() 對象類型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 美国一级黄色片 | 日韩高清中文字幕 | 亚洲九九色 | 成人免费在线 | 国产精品一区三区 | 美女拍拍拍网站 | 亚洲色图综合网 | 香蕉一区 | 国产高清视频 | 国产精品久久久久久久久 | 久久久精品 | 99re视频在线观看 | 天堂在线免费视频 | 熟女毛片 | 日韩福利| 国产在线视频一区二区 | 欧美一区二区另类 | 国产精品区一区二区三 | 一区二区三区四区在线视频 | 午夜日韩 | 日韩精品一区二区三区在线播放 | 高清一区二区三区 | 久久精品网 | 欧美不卡一区二区三区 | 国产一区二区欧美 | 国产精品视频免费观看 | 成人精品一区二区三区 | 中文字幕国产视频 | 亚洲毛片在线 | 日韩中文字幕一区 | 国产精品毛片无码 | 91久久精品国产 | 亚洲精品免费观看 | 久久久在线视频 | 一级免费毛片 | 涩涩视频网站在线观看 | 国产一区二区影院 | 欧美日韩三级 | 人操人人干人 | 亚洲高清在线 | 欧美精品综合 |