我遇到执行此代码的问题。
在可视化工具中调用该函数后,我看到for循环在第32步之后随机退出。
一切似乎进展顺利,而且#34; val"正在被删除。然后,突然,for循环退出并继续其余的逻辑。
def remove(val, xs, limit=None):
xs.sort()
if limit != None and limit > 0:
numval = limit
for i in xs:
if i == val and numval > 0:
xs.remove(i)
numval -= 1
else:
continue
elif limit == None:
for i in xs:
if i == val:
xs.remove(i)
return None
else:
return (xs)
xs=[9,2,9,9,3,9,9,9,4,9,9,9,9,5,9,9,9,9,9,6]
remove(9,xs)
关于为什么会发生这种情况的任何想法?