Python Forum
Many iterations for loop question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Many iterations for loop question
#1
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!
Yoriz write Nov-12-2022, 01:11 AM:
Please post all code, output and errors (in their 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.

Attached Files

.py   pythonforum.py (Size: 4.35 KB / Downloads: 126)
Reply
#2
ignore this reply
Reply
#3
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
Reply
#4
(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.
Reply
#5
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.
Reply
#6
(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.
Reply
#7
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
Reply
#8
(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?
Reply
#9
(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)
Reply
#10
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.
ndc85430 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with creating loops and Iterations for ISO Weeks abecruz92 1 1,311 Dec-29-2022, 12:33 PM
Last Post: Yoriz
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,615 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,809 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  How to print all iterations of min and max for a 2D list alaina 4 2,915 Nov-11-2020, 05:53 AM
Last Post: alaina
  Repeat question (for loop) rs74 7 6,503 Jun-17-2020, 03:17 PM
Last Post: rs74
  Question about running comparisons through loop from input value Sunioj 2 2,414 Oct-15-2019, 03:15 PM
Last Post: jefsummers
  while loop question Tripler 4 2,962 Jul-24-2018, 06:37 AM
Last Post: buran
  Loop question kraven 3 3,642 Sep-10-2017, 07:31 AM
Last Post: wavic
  Question about loop Pires 4 3,584 Jul-23-2017, 03:01 AM
Last Post: Pires
  Udacity while loop question liquidmetalrob 6 5,375 Jul-21-2017, 02:56 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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