Posts: 7
Threads: 2
Joined: Nov 2022
Nov-12-2022, 12:49 AM
(This post was last modified: Nov-12-2022, 06:18 AM by adesimone.)
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!
Posts: 7
Threads: 2
Joined: Nov 2022
Nov-12-2022, 12:50 AM
(This post was last modified: Nov-12-2022, 12:55 AM by adesimone.)
Posts: 6,794
Threads: 20
Joined: Feb 2020
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
Posts: 7
Threads: 2
Joined: Nov 2022
Nov-12-2022, 04:29 AM
(This post was last modified: Nov-12-2022, 12:42 PM by Larz60+.)
(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)
Larz60+ write Nov-12-2022, 12:42 PM:Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Posts: 6,794
Threads: 20
Joined: Feb 2020
Nov-12-2022, 05:07 AM
(This post was last modified: Nov-12-2022, 05:07 AM by deanhystad.)
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.
Posts: 7
Threads: 2
Joined: Nov 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.
Posts: 6,794
Threads: 20
Joined: Feb 2020
Nov-12-2022, 07:05 AM
(This post was last modified: Nov-12-2022, 07:12 AM by deanhystad.)
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
Posts: 7
Threads: 2
Joined: Nov 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?
Posts: 7
Threads: 2
Joined: Nov 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)
Posts: 6,794
Threads: 20
Joined: Feb 2020
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.
|