简体   繁体   中英

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.

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:

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. 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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM