重复for循环的迭代 [英] repeat an iteration of for loop
本文介绍了重复for循环的迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于listOfIds中的eachId,如果由于某种原因,我想重复相同的迭代。 :
#assume here eachId conatins 10
response = makeRequest(eachId)#assume makeRequest函数请求到一个url使用这个ID
if response =='market is closed':
time.sleep(24 * 60 * 60)#sleep for a day
现在当功能从睡眠后醒来一天后(市场(货币交易市场)是开放的)我想恢复我的for循环从 eachId = 10
不是
from eachId = 11
,因为 eachId = 10
尚未作为<$ c处理$ c $>市场被关闭,任何帮助非常感谢。
对于listOfIds中的eachId:
成功= False
,但不成功:
响应= makeRequest(eachId)
如果响应==市场关闭:
time.sleep(24 * 60 * 60)#sleep一天
其他:
成功=真
问题的标题就是线索。 重复是通过迭代实现的,在这种情况下,您可以简单地使用来嵌套
。
if for some reason i want to repeat the same iteration how i can do it in python?
for eachId in listOfIds:
#assume here that eachId conatins 10
response = makeRequest(eachId) #assume that makeRequest function request to a url by using this id
if response == 'market is closed':
time.sleep(24*60*60) #sleep for one day
now when the function wake up from sleep after one day (market (currency trade market) is open) i want to resume my for loop from eachId = 10
not
from eachId = 11
, because eachId = 10
is not yet been processed as market was closed
, any help is highly appreciated thanks.
解决方案
Do it like this:
for eachId in listOfIds:
successful = False
while not successful:
response = makeRequest(eachId)
if response == 'market is closed':
time.sleep(24*60*60) #sleep for one day
else:
successful = True
The title of your question is the clue. Repeating is achieved by iteration, and in this case you can do it simply with a nested while
.
这篇关于重复for循环的迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文