我有一个具有多个功能的代码,代码的迭代次数为 10。def vectfit_auto(f, s, n_poles=5, n_iter=10,loss_ratio=1e-2, rcond=-1,):for _ in range(n_iter): poles, Zeros, H = vectfit_step(f, s, poles) poles_list.append(poles)我想添加一些行到vectfit_step(我的功能之一)如下进行修改:from iteration number of 5 to 10do something我希望代码像以前一样运行,并且我的修改仅从迭代次数 5 到最后应用。我怎样才能做到这一点?谢谢
2 回答

慕婉清6462132
TA贡献1804条经验 获得超2个赞
正如所评论的,您可以在循环中包含一个 if 语句,并且只有在您运行主循环一定次数后才让它运行。
for i in range(6): # 11 - 5
if i == 5:
for i in range(5):
do_something()
# main code here

aluckdog
TA贡献1847条经验 获得超7个赞
#i takes values between begin and (end - 1)
for i in range(begin, end):
do_something()
#In your case start = 5 and end = 11
for i in range(5, 10+1):
do_something(i)
#You might use _, if you are not interested in the value of i
for _ in range(5, 11):
do_something()
添加回答
举报
0/150
提交
取消