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开始。请求专家评论。Python for循环启动计数器初始化
当你说'范围(15)'(例如)时,它确实说'范围(0,15)'。只使用一个参数为你(程序员)简化它,但它确实在说后者。你应该将它简化为'用于范围内的索引(iteration + 1,len(myList)):'。另外,尽量不要像你在这里那样重用变量名称。当你说'在...中索引'时,你重置之前给出的索引值。正如我假设你想的那样,在之前设置索引不会影响'for'循环中的起始值。 index的值被更改为'range(oneArg)':0的默认值,并且带有2个参数,即第一个值。 – erdekhayser