2012-07-09 19 views
3

我想在python中循環遍歷一個循環,但嵌套循環未達到增量元素。如何在python循環中迭代mongo遊標

除了像「hasNext()」那樣使用range以外,還有什麼辦法嗎?

cursor1 = Collection.find({x : {"$gt" : 1}}) 
array1 = [] 
array2 = [] 
print Collection.count() 

for r in range(0, cursor1.count()): 
    first = cursor1.next().get("entity") 
    array2.append() 

    for z in range(len(array2)): 
     print len(original_tweets) 
     if originalEntity.get("id") != duplicated_entity("id"): 
      array2.append(second) 

回答

9

只是想迭代,你自然會在光標的對象,我不明白你想重複使用它和range.next()

cursor1= Collection.find({x : {"$gt" : 1}}) 
for record in cursor1: 
    # do stuff with your record 
+0

好的,如果集合有非整數值我怎麼可以在數組中使用它們? – user1511208 2012-07-09 07:53:33

+0

@ user1511208 ['for .. in ..'loop](http://docs.python.org/tutorial/controlflow.html#for-statements)將遍歷'cursor1'返回的所有記錄。你不需要事先知道密鑰(你似乎在嘗試在原始問題中使用'range')。 – Stennie 2012-07-09 09:28:27