Python for loop start counter initialization

后端 未结 4 1643
后悔当初
后悔当初 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条回答
  •  萌比男神i
    2021-01-06 02:48

    You really should be using enumerate for stuff like this, as you can loop through the index and the value at the same time (which will save you the hassle of using two for-loops).

    for i, j in enumerate(list):
        print i, j
    

    Your inner loop is overriding the variable index that you defined in the first loop.

提交回复
热议问题