Python Forum
Many iterations for loop question - 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: Many iterations for loop question (/thread-38685.html)



Many iterations for loop question - adesimone - Nov-12-2022

Hi all,

I am trying to write a for loop that will iterate through 10 items in the Twaterout list, subtract those from the first item in Tairout, and then move onto the next 10 items and second item respectively. Unfortunately, I cannot figure out the code to make that happen. Below is the code I wrote that only works for the first 10 items in Twaterout.

Please know that this code is not for a coding course--the course relies on code to extensive heat transfer problems. (the professor will happily give us sections of code to assist)

Tairout is a length 20 list and Twaterout is a length 200 list.

Tairout = [800 600]
Twaterout = [30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50]
fluxarray = []
changeinTwater = []

for j in range(len(Tairout)):
    for i in range(10):
        change = Twaterout[i+1]-Twaterout[i]
        changeinTwater.append(change)
    fluxarray.append((Tairout[j]-changeinTwater)/Rpipe15m)
Thank you!


RE: Many iterations for loop question - adesimone - Nov-12-2022

ignore this reply


RE: Many iterations for loop question - deanhystad - Nov-12-2022

Please post code, not links.

Sounds like you want to slice Twaterout into a bunch of length 10 lists. Do you know how to slice lists? If not, lookup "list slice". You might also want to read about Python sets.

I think you can solve the problem with one for loop. Two at the most


RE: Many iterations for loop question - adesimone - Nov-12-2022

(Nov-12-2022, 03:55 AM)deanhystad Wrote: Please post code, not links.

Sounds like you want to slice Twaterout into a bunch of length 10 lists. Do you know how to slice lists? If not, lookup "list slice". You might also want to read about Python sets.

I think you can solve the problem with one for loop. Two at the most

Here is the code. The indents were not copying well--fluxarray.append...is in line with "for i in range(10):
fluxarray = []
changeinTwater = []

for j in range(len(Tairout)):
    for i in range(10):
        change = Twaterout[i+1]-Twaterout[i]
        changeinTwater.append(change)
    fluxarray.append((Tairout[j]-changeinTwater)/Rpipe15m)



RE: Many iterations for loop question - deanhystad - Nov-12-2022

Please wrap code in Python tags as mentioned in Yoriz's reply to your initial post.

Where are Twaterout and Tiarout defined? Could you provide a pared down list, just enough run your code. Maybe 2 and 20 instead of 20 and 200.


RE: Many iterations for loop question - adesimone - Nov-12-2022

(Nov-12-2022, 05:07 AM)deanhystad Wrote: Please wrap code in Python tags as mentioned in Yoriz's reply to your initial post.

Where are Twaterout and Tiarout defined? Could you provide a pared down list, just enough run your code. Maybe 2 and 20 instead of 20 and 200.

I fixed the code in the original post.


RE: Many iterations for loop question - deanhystad - Nov-12-2022

What is this supposed to do:
fluxarray.append((Tairout[j]-changeinTwater)/Rpipe15m)
I am guessing that in the equation "changinTwater" is a scalar. In your program it is a list. Does this calculation belong inside the inner loop, and should it use change instead of changeinTwater?

Rpipe15m is not defined anywhere.

Your other issue is that the index for Twaterout is wrong. If you consume 10 Twaterout changes for each Tairoot, the indices should look like this:
Output:
Tairoot, Twaterout 0, 0..9 1, 10..19 2, 20..29
You were doing this
Output:
Tairoot, Twaterout 0, 0..9 1, 1..10 2, 2..11



RE: Many iterations for loop question - adesimone - Nov-12-2022

(Nov-12-2022, 07:05 AM)deanhystad Wrote: What is this supposed to do:
fluxarray.append((Tairout[j]-changeinTwater)/Rpipe15m)
I am guessing that in the equation "changinTwater" is a scalar. In your program it is a list. Does this calculation belong inside the inner loop, and should it use change instead of changeinTwater?

Rpipe15m is not defined anywhere.

Your other issue is that the index for Twaterout is wrong. If you consume 10 Twaterout changes for each Tairoot, the indices should look like this:
Output:
Tairoot, Twaterout 0, 0..9 1, 10..19 2, 20..29
You were doing this
Output:
Tairoot, Twaterout 0, 0..9 1, 1..10 2, 2..11

Thank you...that's what I have been trying to get the code to do. How do I code the for loops to show those indices?


RE: Many iterations for loop question - adesimone - Nov-12-2022

(Nov-12-2022, 04:57 PM)adesimone Wrote:
(Nov-12-2022, 07:05 AM)deanhystad Wrote: What is this supposed to do:
fluxarray.append((Tairout[j]-changeinTwater)/Rpipe15m)
I am guessing that in the equation "changinTwater" is a scalar. In your program it is a list. Does this calculation belong inside the inner loop, and should it use change instead of changeinTwater?

Rpipe15m is not defined anywhere.

Your other issue is that the index for Twaterout is wrong. If you consume 10 Twaterout changes for each Tairoot, the indices should look like this:
Output:
Tairoot, Twaterout 0, 0..9 1, 10..19 2, 20..29
You were doing this
Output:
Tairoot, Twaterout 0, 0..9 1, 1..10 2, 2..11

Thank you...that's what I have been trying to get the code to do. How do I code the for loops to show those indices?
EDIT: Does this create those indices?
for j in range(len(Tairout)):
    for i in range(10):
        changeinTwater.append(Twaterout[j+i+1]-Twaterout[j+i])
    fluxarray.append((Tairout[j]-changeinTwater)/Rpipe15m)



RE: Many iterations for loop question - deanhystad - Nov-12-2022

Solve that the same way as anything else. First solve with pencil and paper, then translate algorithm to code. Solve problems in a language you understand.