【发布时间】:2013-07-06 00:24:06
【问题描述】:
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)
以上是我为选择排序算法写的一段代码sn-p。但是我看到内部循环开始计数器总是从 0 开始。请求专家评论。
【问题讨论】:
-
当你说
range(15)(例如)时,它实际上是range(0,15)。仅使用一个参数可以为您(程序员)简化它,但实际上是后者。您应该将其简化为for index in range(iteration + 1, len(myList)):。另外,尽量不要像这里那样重用变量名。当您说for index in...时,您重置了之前给它的索引的值。正如我假设你所想的那样,在for循环中设置索引不会影响它的起始值。 index 的值更改为默认值range(oneArg):0,第一个值为 2 个参数。