Python Forum
How can I run a function inside a loop every 24 values of the loop iteration range?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I run a function inside a loop every 24 values of the loop iteration range?
#1
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
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop to find the best combination/score KoinKoin 21 26,894 Jan-05-2023, 10:31 AM
Last Post: KoinKoin
  Many iterations for loop question adesimone 9 1,834 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,590 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  while loop idddj 8 1,683 Oct-03-2022, 05:03 PM
Last Post: jefsummers
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,796 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Function combining file manipulation and loop Leyo 5 1,753 Mar-23-2022, 09:47 AM
Last Post: Leyo
  Using If Statements Instead of While Loop in Simple Game Program new_coder_231013 5 3,165 Dec-14-2021, 12:23 AM
Last Post: supuflounder
Big Grin for loop nadun 3 1,865 Nov-22-2021, 03:36 PM
Last Post: deanhystad
  How to compile following python loop program reinispl 3 1,948 Oct-27-2021, 01:57 PM
Last Post: DeaD_EyE
  Printing During a Loop apeltes 16 5,236 Oct-21-2021, 12:19 AM
Last Post: apeltes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020