python: restarting a loop

后端 未结 5 728
日久生厌
日久生厌 2020-11-30 08:50

i have:

for i in range(2,n):
    if(something):
       do something
    else:
       do something else
       i = 2 **restart the loop

But

相关标签:
5条回答
  • 2020-11-30 09:15

    Changing the index variable i from within the loop is unlikely to do what you expect. You may need to use a while loop instead, and control the incrementing of the loop variable yourself. Each time around the for loop, i is reassigned with the next value from range(). So something like:

    i = 2
    while i < n:
        if(something):
            do something
        else:
            do something else
            i = 2 # restart the loop
            continue
        i += 1
    

    In my example, the continue statement jumps back up to the top of the loop, skipping the i += 1 statement for that iteration. Otherwise, i is incremented as you would expect (same as the for loop).

    0 讨论(0)
  • 2020-11-30 09:17

    Here is an example using a generator's send() method:

    def restartable(seq):
        while True:
            for item in seq:
                restart = yield item
                if restart:
                    break
            else:
                raise StopIteration
    

    Example Usage:

    x = [1, 2, 3, 4, 5]
    total = 0
    r = restartable(x)
    for item in r:
        if item == 5 and total < 100:
            total += r.send(True) 
        else:
            total += item
    
    0 讨论(0)
  • 2020-11-30 09:18
    a = ['1', '2', '3']
    ls = []
    count = False
    
    while ls != a :
        print(a[count])
        if a[count] != a[-1] :
            count = count + 1
        else :
            count = False
    

    Restart while loop.

    0 讨论(0)
  • 2020-11-30 09:24

    You may want to consider using a different type of loop where that logic is applicable, because it is the most obvious answer.

    perhaps a:

    i=2
    while i < n:
        if something:
           do something
           i += 1
        else: 
           do something else  
           i = 2 #restart the loop  
    
    0 讨论(0)
  • 2020-11-30 09:25

    Just wanted to post an alternative which might be more genearally usable. Most of the existing solutions use a loop index to avoid this. But you don't have to use an index - the key here is that unlike a for loop, where the loop variable is hidden, the loop variable is exposed.

    You can do very similar things with iterators/generators:

    x = [1,2,3,4,5,6]
    xi = iter(x)
    ival = xi.next()
    while not exit_condition(ival):
        # Do some ival stuff
        if ival == 4:
            xi = iter(x)
        ival = xi.next()
    

    It's not as clean, but still retains the ability to write to the loop iterator itself.

    Usually, when you think you want to do this, your algorithm is wrong, and you should rewrite it more cleanly. Probably what you really want to do is use a generator/coroutine instead. But it is at least possible.

    0 讨论(0)
提交回复
热议问题