我在我的数据库中有500条记录,我正在将它们全部加载到字典元素的python列表中。
我正在调用一个方法,该方法遍历列表中的所有字典元素,并对每个记录执行一些操作。在遍历时,我注意到在遍历了一半的元素之后,逻辑退出了方法,并在下半部分重新启动。
我试着把它作为一个例子。(我无法复制确切的代码,因为这是不可能的)
例如:
def loadrec():
reading from database and appending rows to a global list(mylist)
def myrun():
print "****** Execution Started **********"
for row in mylist:
doing some operations and printing a string
if __name__=="__main__":
i=0
while (i>=0): #This never ends(to make the script run forever)
loadrec()
myrun()
print "****** Execution Ended ************"
Result:
****** Execution Started **********
printed 250 records
****** Execution Ended ************
****** Execution Started **********
printed 125 records
****** Execution Ended ************
****** Execution Started **********
printed 62 records
****** Execution Ended ************
****** Execution Started **********
printed 31 records
****** Execution Ended ************
****** Execution Started **********
printed 15 records
****** Execution Ended ************
****** Execution Started **********
printed 7 records
****** Execution Ended ************
****** Execution Started **********
printed 1 record
****** Execution Ended ************我不知道为什么每次只处理总记录的一半。但是,最后,它正在处理所有的记录。
我试过检查python列表的最大大小或内存是否有问题,但它们似乎都不是可能的场景。
会很高兴知道任何可能是什么原因。
发布于 2017-10-06 04:36:55
我要在黑暗中疯狂地捅一刀。
你在做这样的事情:
def myrun():
print "****** Execution Started **********"
for row in mylist:
#Some processing here
myList.remove(row)您不能修改正在迭代的列表。这将只占每次处理列表的一半。您需要重构您的代码。如果没有你给我们展示你的代码,我甚至不会尝试去做重构。
https://stackoverflow.com/questions/46598205
复制相似问题