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
Exclamation How to get program to loop if input date is in the past? xander_thornton45 3 1,698 Oct-11-2024, 10:08 AM
Last Post: Pedroski55
  Loop problem Milczek 1 1,157 Jul-22-2024, 03:34 AM
Last Post: deanhystad
  Loop to find the best combination/score KoinKoin 21 37,891 Jan-05-2023, 10:31 AM
Last Post: KoinKoin
  Many iterations for loop question adesimone 9 3,531 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 4,648 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  while loop idddj 8 3,216 Oct-03-2022, 05:03 PM
Last Post: jefsummers
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 3,417 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Function combining file manipulation and loop Leyo 5 2,834 Mar-23-2022, 09:47 AM
Last Post: Leyo
  Using If Statements Instead of While Loop in Simple Game Program new_coder_231013 5 5,692 Dec-14-2021, 12:23 AM
Last Post: supuflounder
Big Grin for loop nadun 3 2,735 Nov-22-2021, 03:36 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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