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