为了账号安全,请及时绑定邮箱和手机立即绑定

python - 当代码在python中运行x次时,如何在代码中运行函数的特定部分?

python - 当代码在python中运行x次时,如何在代码中运行函数的特定部分?

拉丁的传说 2021-11-16 18:16:16
我有一个具有多个功能的代码,代码的迭代次数为 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


查看完整回答
反对 回复 2021-11-16
?
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()


查看完整回答
反对 回复 2021-11-16
  • 2 回答
  • 0 关注
  • 229 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信