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

可以在 Python 中重置迭代器嗎?

Can iterators be reset in Python?(可以在 Python 中重置迭代器嗎?)
本文介紹了可以在 Python 中重置迭代器嗎?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我可以在 Python 中重置迭代器/生成器嗎?我正在使用 DictReader 并希望將其重置為文件的開頭.

Can I reset an iterator / generator in Python? I am using DictReader and would like to reset it to the beginning of the file.

推薦答案

我看到很多答案建議 itertools.tee,但這忽略了文檔中的一個(gè)重要警告:

I see many answers suggesting itertools.tee, but that's ignoring one crucial warning in the docs for it:

此迭代工具可能需要大量輔助存儲(chǔ)(取決于如何需要大量臨時(shí)數(shù)據(jù)存儲(chǔ)).一般來(lái)說(shuō),如果一個(gè)迭代器使用之前的大部分或全部數(shù)據(jù)另一個(gè)迭代器啟動(dòng),它更快使用 list() 而不是 tee().

This itertool may require significant auxiliary storage (depending on how much temporary data needs to be stored). In general, if one iterator uses most or all of the data before another iterator starts, it is faster to use list() instead of tee().

基本上,tee 是為這樣的情況設(shè)計(jì)的 - 相反,他們說(shuō)的是同一個(gè)附近"(一些項(xiàng)目彼此落后或領(lǐng)先).不適合OP的從頭重做"的問(wèn)題.

Basically, tee is designed for those situation where two (or more) clones of one iterator, while "getting out of sync" with each other, don't do so by much -- rather, they say in the same "vicinity" (a few items behind or ahead of each other). Not suitable for the OP's problem of "redo from the start".

L = list(DictReader(...)) 另一方面是非常合適的,只要字典列表可以舒適地放入內(nèi)存中.可以隨時(shí)使用 iter(L) 制作一個(gè)新的從頭開始的迭代器"(非常輕量級(jí)和低開銷),并部分或全部使用而不影響新的或現(xiàn)有的;其他訪問(wèn)模式也很容易獲得.

L = list(DictReader(...)) on the other hand is perfectly suitable, as long as the list of dicts can fit comfortably in memory. A new "iterator from the start" (very lightweight and low-overhead) can be made at any time with iter(L), and used in part or in whole without affecting new or existing ones; other access patterns are also easily available.

正如幾個(gè)答案正確指出的那樣,在 csv 的特定情況下,您還可以 .seek(0) 底層文件對(duì)象(一種相當(dāng)特殊的情況).盡管目前確實(shí)有效,但我不確定是否已記錄并保證.僅對(duì)于真正巨大的 csv 文件可能值得考慮,其中我推薦的 list 因?yàn)橐话惴椒〞?huì)占用太大的內(nèi)存.

As several answers rightly remarked, in the specific case of csv you can also .seek(0) the underlying file object (a rather special case). I'm not sure that's documented and guaranteed, though it does currently work; it would probably be worth considering only for truly huge csv files, in which the list I recommmend as the general approach would have too large a memory footprint.

這篇關(guān)于可以在 Python 中重置迭代器嗎?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(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 檢測(cè)和跟蹤人員?)
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ù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产日韩免费视频 | 午夜一区 | 久久久蜜臀国产一区二区 | 三a毛片 | 激情欧美一区二区三区中文字幕 | 亚洲一区二区三区 | 美女在线国产 | 国产免费观看久久黄av片涩av | 国产亚洲精品一区二区三区 | 日韩第一区 | 在线日韩福利 | 亚洲性视频网站 | 亚洲成人免费网址 | 日本三级网址 | 久久久精彩视频 | av黄色在线 | 超碰av免费 | 中文字幕在线免费观看 | 精品国产免费人成在线观看 | 国产精品爱久久久久久久 | 国产乱码精品一区二区三区五月婷 | 国产激情一区二区三区 | av电影手机版 | 日韩中文字幕在线观看 | 国产视频中文字幕 | 国产不卡在线播放 | 日本精品一区二区三区在线观看视频 | 国产精品久久久久一区二区三区 | 午夜精品久久久久久久99黑人 | 婷婷去俺也去 | 99精品欧美一区二区蜜桃免费 | 国产精品欧美一区二区三区 | 精品国产一区二区三区成人影院 | 天堂在线91| 99国产精品久久久久久久 | 欧美9999| 久久久久国 | 一区二区日韩 | 亚洲综合色婷婷 | av影音在线 | 毛片黄片免费看 |