重复for循环的迭代

时间:2011-09-03 15:22:13

标签: python for-loop

如果由于某种原因我想重复相同的迭代,我怎么能在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

现在当功能在一天之后从睡眠状态唤醒(市场(货币交易市场)开启)我想从eachId = 10 noteachId = 11恢复我的for循环,因为eachId = 10尚未处理为market was closed,我们非常感谢任何帮助。

4 个答案:

答案 0 :(得分:25)

这样做:

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

你问题的标题是线索。 重复是通过迭代实现的,在这种情况下,您只需使用嵌套的while即可。

答案 1 :(得分:6)

使用while循环?

counter = 0
while counter < len(listOfIds):
  # do processing
  counter = counter + 1

如果你得到'市场关闭',就不要增加。

答案 2 :(得分:0)

for eachId in listOfIds:
    while makeRequest(eachId) == 'market is closed':
       time.sleep(24*60*60) #sleep for one day

正如@David所说,如果您不需要捕获response

答案 3 :(得分:0)

i = 0
while i < len(listOfIds):
    eachId = listOfIds[i]
    #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
    else:
       i += 1