【发布时间】:2015-10-08 15:10:58
【问题描述】:
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循环的正确用法吗?
我已经改变了一点。这是正确的吗?
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]))
【问题讨论】:
-
你是什么意思“正确”?有用吗?
-
它确实有效,但它说 y 是未使用的变量。
-
好吧,看看你的代码 - 你有没有使用
y? -
意思是即使我删除了整个 y... s: ,结果还是一样的,对吧?
-
何不试试看?