def insert3(x,ss):
left = [] #why this need to add properly by list, or not it just return the recent result.
while ss!= []:
for y in ss:
if x<= ss[0]:
return left + [x] + ss[0:]
else:
ss, left = ss[1:], left + [ss[0]]
return left + ss + [x]
print(insert3(6,[2,4,5,7,8]))
這是循環for函數的正確用法嗎?在列表中使用for循環對數字進行排序
我改變了一點。它是否正確?
def insert3(x,ss):
left = []
for y in ss:
if x<= ss[0]:
return left + [x] + ss[0:]
else:
ss, left = ss[1:], left + [ss[0]]
return left + ss + [x]
print(insert3(6,[2,4,5,7,8]))
你是什麼意思*「正確」*?它工作嗎? – jonrsharpe
它確實有效,但它說y是未使用的變量。 –
好吧,看看你的代碼 - 你有沒有使用*'y'? – jonrsharpe