Python Forum
How can I run a function inside a loop every 24 values of the loop iteration range? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How can I run a function inside a loop every 24 values of the loop iteration range? (/thread-21195.html)



How can I run a function inside a loop every 24 values of the loop iteration range? - mcva - Sep-18-2019

How can I run a function inside a loop every 24 values of the loop iteration range?

I only want to TempLake[i]=fix_profile(Nlayers,TempTDMA,Areat0) every 24 values, else I want TempLake[i]=TempTDMA

N=175312

for i in range(1,N+1):
    if i == 1:
        TempLake[i]=TempTDMA
    if i == 2:
        TempLake[i]=TempTDMA
        
        ...
        
    if i == 25:
        TempLake[i]=fix_profile(Nlayers,TempTDMA,Areat0)
    if i == 26:
        TempLake[i]=TempTDMA
        ...
        
    if i == 49:
        TempLake[i]=fix_profile(Nlayers,TempTDMA,Areat0)
I have tried this:

N=175312
for i in range(1,N):
    for j in range(1,N,24):
        if i == j:
but this solution increases a lot the overall running time.

Thank you


RE: How can I run a function inside a loop every 24 values of the loop iteration range? - buran - Sep-18-2019

your example suggest you want to run it on 25th, next 24th element
If it was every 24th, with i starting at 1 it would be 24, 48th, 72nd, etc.
if we assume it's 25, 49, 73, etc...
then
if not (i-1) % 24:
    TempLake[i]=fix_profile(Nlayers,TempTDMA,Areat0)
else:
    TempLake[i]=TempTDMA
% is moduo operator and the result is the reminder. if i-1 is divisible by 24, the reminder is 0, so not 0 is evaluated True