我在Python/PyMongo中有奇怪的行为。
dbh = self.__connection__['test']
first = dbh['test_1']
second = dbh['test_2']
first_collection_records=first.find()
second_collection_records=second.find()
index_f=first_collection_records.count() //20
index_s=second_collection_records.count() //120
i=0
for f in first_collection_records:
for s in second_collection_records:
i=i+1
print i而且它只打印120次(1..120),而不是20x120次。有人能告诉我为什么它不遍历外部集合吗?我打印了结果,它总是只需要外部的第一个,并在内部集合上迭代。(我发布了我在代码20和120中获得的计数,我尝试了xrange和fetch by index,但什么都没有)
发布于 2012-03-20 23:43:59
如果你想为每个first_collection_records迭代second_collection_records,你可以使用:
i=0
for f in first_collection_records:
second_collection_records.rewind() #Reset second_collection_records's iterator
for s in second_collection_records:
i=i+1
print i.rewind()将游标重置为新状态,使您能够再次在second_collection_records中检索数据。
说明:
second.find()返回包含迭代器的Cursor对象。
当游标的迭代器到达其末尾时,它不再返回任何内容。
因此:
for f in first_collection_records: #20实际上确实迭代了20次,但由于内部:
for s in second_collection_records:已经迭代了返回的所有对象,第二次调用时,second_collection_records不再返回任何内容,因此里面的代码(i=i+1,print...)不会被执行。
您可以像这样尝试:
i = 0
for f in first_collection_records:
print "in f"
for s in second_collection_records:
print "inside s"你会得到一个结果:
inside f
inside s
inside s
...
inside s
inside f <- since s has nothing left to be iterated,
(second_collection_records actually raised StopIteration such in generator),
code inside for s in second_collection_records: is no longer executed
inside f
inside f深度解释:
这一行:
for s in second_collection_records: 这里的循环实际上是通过Cursor对象的next()方法工作的,如下所示:调用second_collection_records.next()直到second_collection_records引发StopIteration异常(在Python生成器和for循环中,StopIteration被捕获,for循环中的代码将不会被执行)。因此,在first_collection_records的第二个til last循环中,second_collection_records.next()实际上为内部循环引发了StopIteration,而不是执行代码。
通过这样做,我们可以很容易地观察到这种行为:
for f in first_collection_records:
print "inside f"
second_collection_records.next()
for s in second_collection_records:
print "inside s"结果是:
inside f
inside s
...
inside s
inside f
Traceback (most recent call last):
... , in next
raise StopIteration
StopIterationhttps://stackoverflow.com/questions/9789601
复制相似问题