Go to the RMD, PDF, or HTML version of this file. Go back to Python Code Examples Repository (bookdown site) or the pyfan Package (API).
import numpy as np
Loop over integers up to a maximum number. Take certain actions at the beginning and at the end of the loop. Note that in python, indexing starts at 0, and ends 1 less than the length. So below, np.arange(4) generates a list that starts at 0 and goes to 3 of length 4. The last element is not 4. Ending index is 4-1, dont forget the -1.
# Maximum number
it_bisection_iter = 4
# np arange
ls_it_loop = np.arange(it_bisection_iter)
print(f'{ls_it_loop=}')
# Iteration
## ls_it_loop=array([0, 1, 2, 3])
for it_bisection_ctr in ls_it_loop:
if it_bisection_ctr == 0:
print('starting index')
elif it_bisection_ctr == it_bisection_iter-1:
print('ending index')
print(f'{it_bisection_ctr=}')
## starting index
## it_bisection_ctr=0
## it_bisection_ctr=1
## it_bisection_ctr=2
## ending index
## it_bisection_ctr=3
We have numbers between 0 and 99, these indicate different estimation specifications, where the digit number is the estimation tolerance level, and the tens number is the minimization algorithm.
ls_it_esti_optsalgo = [0, 1, 10, 15, 23, 89, 90, 99, 900, 901, 999, 1000]
for it_esti_optsalgo in ls_it_esti_optsalgo:
it_esti_optsalgo_tens = int(np.floor(it_esti_optsalgo/10))
it_esti_optsalgo_digits = it_esti_optsalgo%10
print(f'{it_esti_optsalgo_tens=}, {it_esti_optsalgo_digits=}')
## it_esti_optsalgo_tens=0, it_esti_optsalgo_digits=0
## it_esti_optsalgo_tens=0, it_esti_optsalgo_digits=1
## it_esti_optsalgo_tens=1, it_esti_optsalgo_digits=0
## it_esti_optsalgo_tens=1, it_esti_optsalgo_digits=5
## it_esti_optsalgo_tens=2, it_esti_optsalgo_digits=3
## it_esti_optsalgo_tens=8, it_esti_optsalgo_digits=9
## it_esti_optsalgo_tens=9, it_esti_optsalgo_digits=0
## it_esti_optsalgo_tens=9, it_esti_optsalgo_digits=9
## it_esti_optsalgo_tens=90, it_esti_optsalgo_digits=0
## it_esti_optsalgo_tens=90, it_esti_optsalgo_digits=1
## it_esti_optsalgo_tens=99, it_esti_optsalgo_digits=9
## it_esti_optsalgo_tens=100, it_esti_optsalgo_digits=0