老实说,我在这里有点困惑,为什么我不能在相同的数据上迭代两次?
def _view(self,dbName):
db = self.dictDatabases[dbName]
data = db[3]
for row in data:
print("doing this one time")
for row in data:
print("doing this two times")
Run Code Online (Sandbox Code Playgroud)
这将打印出"一次这样做"几次(因为数据有几行),但它根本不会打印出"这样做两次"......
我第一次迭代数据工作正常,但第二次当我运行最后一个列表"for data in data"时,这没有返回...所以执行它一次工作但不是两次......?
仅供参考 - 数据是一个csv.reader对象(如果是这样的原因)......
有一个自定义迭代器的问题,它只会迭代文件一次.我seek(0)在迭代之间调用相关的文件对象,但是在第二次运行StopIteration的第一次调用时抛出next().我觉得我忽略了一些显而易见的事情,但我会对此有一些新的看法:
class MappedIterator(object):
"""
Given an iterator of dicts or objects and a attribute mapping dict,
will make the objects accessible via the desired interface.
Currently it will only produce dictionaries with string values. Can be
made to support actual objects later on. Somehow... :D
"""
def __init__(self, obj=None, mapping={}, *args, **kwargs):
self._obj = obj
self._mapping = mapping
self.cnt = 0
def __iter__(self):
return self
def reset(self):
self.cnt = 0
def next(self):
try:
try: …Run Code Online (Sandbox Code Playgroud)