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

时间:2023-01-06
本文介绍了可以在 Python 中重置迭代器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我可以在 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,但这忽略了文档中的一个重要警告:

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

此迭代工具可能需要大量辅助存储(取决于如何需要大量临时数据存储).一般来说,如果一个迭代器使用之前的大部分或全部数据另一个迭代器启动,它更快使用 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 是为这样的情况设计的 - 相反,他们说的是同一个附近"(一些项目彼此落后或领先).不适合OP的从头重做"的问题.

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(...)) 另一方面是非常合适的,只要字典列表可以舒适地放入内存中.可以随时使用 iter(L) 制作一个新的从头开始的迭代器"(非常轻量级和低开销),并部分或全部使用而不影响新的或现有的;其他访问模式也很容易获得.

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.

正如几个答案正确指出的那样,在 csv 的特定情况下,您还可以 .seek(0) 底层文件对象(一种相当特殊的情况).尽管目前确实有效,但我不确定是否已记录并保证.仅对于真正巨大的 csv 文件可能值得考虑,其中我推荐的 list 因为一般方法会占用太大的内存.

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.

这篇关于可以在 Python 中重置迭代器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:在 Python 中获取迭代器中的元素数量 下一篇:为什么在类上定义 __getitem__ 使其在 python 中可迭代?

相关文章

最新文章