[英]How to run specific part of a function in a code when code has been run for x times in python?
I have a code that has several functions and the number of iteration of code is 10. 我有一个具有多个功能的代码,代码的迭代次数为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)
I want to add some lines to the vectfit_step
(one of my functions) as below for modification: 我想向
vectfit_step
(我的功能之一)添加一些行,如下所示:
from iteration number of 5 to 10
do something
I want that the code runs like before, and my modification be applied only from the iteration number of 5 to the end. 我希望代码像以前一样运行,并且我的修改仅适用于从迭代次数5到结束的情况。 How can I do that?
我怎样才能做到这一点? Thanks
谢谢
As commented, you can include an if statement within your loop and only let it run once you've run the main loop a certain amount of times. 如前所述,您可以在循环中包含if语句,并且仅在运行主循环一定次数后才让它运行。
for i in range(6): # 11 - 5
if i == 5:
for i in range(5):
do_something()
# main code here
#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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.