Python for loop start counter initialization

后端 未结 4 1651
后悔当初
后悔当初 2021-01-06 02:08
for iteration in range(len(list) - 1):
  index = iteration +1 #This is the line which has no effect on the inner loop
  for index in range(len(list)):
    if list[it         


        
4条回答
  •  遥遥无期
    2021-01-06 03:03

    The for index in range(len(list)) loop executes the loop body with index first set to 0, then 1, then 2, etc. up to len(list) - 1. The previous value of index is ignored and overwritten. If you want index to start at iteration + 1, use the 2-argument form of range:

    for index in range(iteration + 1, len(list)):
    

提交回复
热议问题