Iterate again within the for loop

前端 未结 7 1098
走了就别回头了
走了就别回头了 2021-01-21 07:50

is there a way to iterate again within the for loop? For example:

for x in list:
  if(condition):
      #I\'d like to grab the next iteration of the lis         


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-21 08:30

    You could do something like this:

    a = [1,2,3,4,5]
    b = iter(a)
    
    try:
        while True:
            c = b.next()
            if (condition):
                c = b.next()
    except StopIteration:
        pass
    

提交回复
热议问题