Home > Enterprise >  Is there a way to skip few commands in a for loop by checking the condition before?
Is there a way to skip few commands in a for loop by checking the condition before?

Time:02-24

I want to ask if there is a way to skip few commands in a for loop by checking the condition before.

Here is an example to you understand my question.


I want to add 1 to a and append a to a list a_list for n_iter times.
But, appending a should be done only if flag is True.

I think the code can be wrriten in two ways.


CODE 1

a = 0
a_list = []
n_iter = 10
flag = False
for _ in range(n_iter):
    a  = 1
    if flag:
        a_list.append(a)

CODE 2

a = 0
a_list = []
n_iter = 10
flag = False
if flag:
    for _ in range(n_iter):
        a  = 1
        a_list.append(a)
else:
    for _ in range(n_iter):
        a  = 1

The CODE 1 has fewer lines, but it requires to check the condition in every loop, which makes the computation time larger compared to CODE 2.

Is there a way to simplify CODE 2 so that it has similar number of lines as CODE 1?

The code I'm writting has a numerous number of lines in a for loop and there are only few lines that are dependent on some flags. Since this loop has to be done a billion times, I want to reduce down the computational cost as much as I can.


As @kingkupps commented that it seems like an XY problem, I would like to give the actual problem I'm doing.

I have analysis data files which have the format as below.

<node no.>, <x-coord.>, <y-coord.>, <z-coord.>, <v-x>, <v-y>, <v-z>, ...
1,-1.758000000E 01, 7.859070503E-01,-6.661221083E 00, 1.853448047E 01, 0.000000000E 00, 0.000000000E 00, ...
2,-1.758000000E 01, 6.714303868E-01,-6.517660165E 00, 1.736286209E 01, 0.000000000E 00, 0.000000000E 00, ...
3,-1.758000000E 01, 8.324907476E-01,-6.482755077E 00, 1.709871564E 01, 0.000000000E 00, 0.000000000E 00, ...
4,-1.758000000E 01, 6.145584633E-01,-6.692237928E 00, 1.872061756E 01, 0.000000000E 00, 0.000000000E 00, ...
...

Beyond the velocity data, there are temperature, pressure, heat flux, and heat transfer coefficient data.

The data files have the same node numbers and the coordinates. Thus, I don't want to read the node nubmers and the coordinates from the second analysis file.

So I wrote a function to read the data as below.


CODE 3

def read_result_file(file_name: str, is_read_node=False):
    if is_read_node:
        x_arr = []
    else:
        x_arr = None
    v_arr, T_arr, press_arr, heat_flux_arr, HTC_arr = [], [], [], [], []

    with open(file_name, 'r') as f:
        if is_read_node:
            for line in f:
                line = line.split(',')
                x = list(map(float, line[1:4]))
                v = list(map(float, line[4:8]))
                T = float(line[8])
                press = float(line[9])
                heat_flux = float(line[10:14])
                HTC = float(line[14])
                x_arr.append(x)
                v_arr.append(v)
                T_arr.append(T)
                press_arr.append(press)
                heat_flux_arr.append(heat_flux)
                HTC_arr.append(HTC)
        else:
            for line in f:
                line = line.split(',')
                v = list(map(float, line[4:8]))
                T = float(line[8])
                press = float(line[9])
                heat_flux = float(line[10:14])
                HTC = float(line[14])
                v_arr.append(v)
                T_arr.append(T)
                press_arr.append(press)
                heat_flux_arr.append(heat_flux)
                HTC_arr.append(HTC)

    v_arr = np.array(v_arr)
    T_arr = np.array(T_arr)
    press_arr = np.array(press_arr)
    heat_flux_arr = np.array(heat_flux_arr)
    HTC_arr = np.array(HTC_arr)
    if is_read_node:
        x_arr = np.array(x_arr)

    return v_arr, T_arr, press_arr, heat_flux_arr, HTC_arr, x_arr

In the for loops in CODE 3, there is only one difference of reading nodal coordinates. So, I want to reduce the length of the number of lines of the code while not increasing the number of condition checks.

CodePudding user response:

Since you are looping through the items in the list every time no matter the value of the flag, you can shorten it to the following using list comprehensions but I don't know how it will affect memory usage a with large number of items:

a = 0
a_list = []
n_iter = 10
flag = False

result = [x 1 for x in range(n_iter)]
a  = len(result)

if flag:
    a_list.extend(result)

print(a_list)
print(a)

If flag is False it prints:

[]
10

If flag is True it prints:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
10

CodePudding user response:

in the CODE2, the flag is only being checked once and then rest of the for loop is being carried out. I think this is at least the number of lines that will be required, else where are you going to check the flag.

  • Related