Python for循环启动计数器初始化 [英] Python for loop start counter initialization
本文介绍了Python for循环启动计数器初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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[iteration] > list[index]:
newmin = list[index]
newminindex = index
if iteration != newminindex :
swapnumbers(list,iteration, newminindex)
以上是我为选择排序算法编写的代码段.但是,我看到内循环开始计数器始终从0开始.请求专家评论.
The above is a code snippet I wrote for selection sort algorithm. However I see the inner loop start counter always starting from 0. Request for expert comment.
推荐答案
for index in range(len(list))
循环执行循环主体,首先将index
设置为0
,然后设置为1
,然后设置为2
,依此类推.到len(list) - 1
. index
的先前值将被忽略并覆盖.如果要index
从iteration + 1
开始,请使用range
的2参数形式:
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)):
这篇关于Python for循环启动计数器初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文