python for increment inner loop

前端 未结 5 1673
轻奢々
轻奢々 2020-12-09 10:07

How to increment the outer iterator from the inner loop?

To be more precise:

  for i in range(0,6):
    print i
    for j in range(0,5):
      i = i         


        
相关标签:
5条回答
  • for a in range(1):
    
        for b in range(3):
            a = b*2
            print(a)
    

    As per your question, you want to iterate the outer loop with help of the inner loop.

    1. In outer loop, we are iterating the inner loop 1 time.
    2. In the inner loop, we are iterating the 3 digits which are in the multiple of 2, starting from 0.

      Output:
      0
      2
      4
      
    0 讨论(0)
  • 2020-12-09 10:50

    It seems that you want to use step parameter of range function. From documentation:

    range(start, stop[, step]) This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns a list of plain integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not be zero (or else ValueError is raised). Example:

     >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
     >>> range(1, 11) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
     >>> range(0, 30, 5) [0, 5, 10, 15, 20, 25]
     >>> range(0, 10, 3) [0, 3, 6, 9]
     >>> range(0, -10, -1) [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
     >>> range(0) []
     >>> range(1, 0) []
    

    In your case to get [0,2,4] you can use:

    range(0,6,2)
    

    OR in your case when is a var:

    idx = None
    for i in range(len(str1)):
        if idx and i < idx:
            continue
        for j in range(len(str2)):
            if str1[i+j] != str2[j]:
                break
        else:
            idx = i+j
    
    0 讨论(0)
  • 2020-12-09 10:51

    You might just be better of using while loops rather than for loops for this. I translated your code directly from the java code.

    str1 = "ababa"
    str2 = "aba"
    i = 0
    
    while i < len(str1):
      j = 0
      while j < len(str2):
        if not str1[i+j] == str1[j]:
          break
        if j == (len(str2) -1):
          i += len(str2)
        j+=1  
      i+=1
    
    0 讨论(0)
  • 2020-12-09 10:54

    I read all the above answers and those are actually good.

    look at this code:

    for i in range(1, 4):
        print("Before change:", i)
        i = 20 # changing i variable
        print("After change:", i) # this line will always print 20
    

    When we execute above code the output is like below,

    Before Change: 1
    After change: 20
    Before Change: 2
    After change: 20
    Before Change: 3
    After change: 20
    

    in python for loop is not trying to increase i value. for loop is just assign values to i which we gave. Using range(4) what we are doing is we give the values to for loop which need assign to the i.

    You can use while loop instead of for loop to do same thing what you want,

    i = 0
    while i < 6:
        print(i)
        j = 0
        while j < 5:
            i += 2 # to increase `i` by 2
    

    This will give,

    0
    2
    4
    

    Thank you !

    0 讨论(0)
  • 2020-12-09 11:12

    In python, for loops iterate over iterables, instead of incrementing a counter, so you have a couple choices. Using a skip flag like Artsiom recommended is one way to do it. Another option is to make a generator from your range and manually advance it by discarding an element using next().

    iGen = (i for i in range(0, 6))
    for i in iGen:
        print i
        if not i % 2:
            iGen.next()
    

    But this isn't quite complete because next() might throw a StopIteration if it reaches the end of the range, so you have to add some logic to detect that and break out of the outer loop if that happens.

    In the end, I'd probably go with aw4ully's solution with the while loops.

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