0

Backstory: I'm fairly new to python, and have only ever done things in MATLAB prior.

I am looking to take a specific value from a table based off of data I have.

The data I have is

Temperatures = [0.8,0.1,-0.8,-1.4,-1.7,-1.5,-2,-1.7,-1.7,-1.3,-0.7,-0.2,0.3,1.4,1.4,1.5,1.2,1,0.9,1.3,1.7,1.7,1.6,1.6]

Hour of the Day = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]

This is all data for a Monday.

My Monday table looks like this:

Temp             |   Hr0   |   Hr1   |   Hr2 ...

-15 < t <= -10   |   0.01  |  0.02   |   0.06 ...

-10 < t <= -5    |   0.04  |  0.03   |   0.2 ...

with the Temperatures increment by +5 until 30, and the hours of the day until 23. The values in the table are constants that I would like to call based off of the temperature and hour.

For example, I'd like to be able to say:

print(monday(1,1)) = 0.01

I would also be doing this for everyday of the week for a mass data analysis, thus the need for it to be efficient.

What I've done so far:

So i have stored all of my tables in dictionaries that look kind of like this:

monday_hr0 = [0.01,0.04, ... ]

So first by column then calling them by the temperature value.

What I have now is a bunch of loops that looks like this:

for i in range (0,365):
   for j in range (0,24):
      if Day[i] = monday
         if hr[i+24*j] = 0
            if temp[i] = -15
               constant.append(monday_hr1[0])
            ...
         if hr[i+24*j] = 1
            if temp[i] = -15
               constant.append(monday_hr2[0])
            ...
         ...
      elif Day[i] = tuesday
         if hr[i+24*j] = 0
            if temp[i] = -15
               constant.append(tuesday_hr1[0])
            ...
         if hr[i+24*j] = 1
            if temp[i] = -15
               constant.append(tuesday_hr2[0])
            ...
         ...
       ...

I'm basically saying here if it's a monday, use this table. Then if it's this hour use this column. Then if it's this temperature, use this cell. This is VERY VERY inefficient however.

I'm sure there's a quicker way but I can't wrap my head around it. Thank you very much for your help!

  • There's a lot of repetition in your code. One way to reduce the number of loops would be to use Dictionaries to store each day's table, and start by picking out what day it is. – Owen Hempel Jun 20 '17 at 10:19
  • I put my tables in dictionaries prior, but because I'm analyzing more than just a single day, I would like to also have my loop go through the days as well. Does that make sense? – Justin Taylor Jun 20 '17 at 10:22
  • I guess how would I reduce the number of loops using dictionaries then. I could very well just be using them wrong. – Justin Taylor Jun 20 '17 at 10:23
  • I see what you mean. Since your temperatures and hours increment by regular amounts, you should be able to figure out what the index of the temperature you want is. What I mean by that is if your temps increase by 5, and you want the value for 15, you know that will be the 3rd element in the list. – Owen Hempel Jun 20 '17 at 10:31
  • Also, the code you've posted iterates through the hours in a day (multiplied by 24), plus the day of the year, and looks at the element of temps corresponding to the day. Is that what you want? – Owen Hempel Jun 20 '17 at 10:33
  • Yes it is what I want so I can narrow it down. I'm asking if there may be a better way to do that? – Justin Taylor Jun 20 '17 at 11:30
  • You have your temperatures in a table (or something like that, I don't understand the contents of your table--are they probabilities?), and your temperatures in a list. You have a heinously large number of iterations because you're iterating over the whole thing so many times. What I'm asking you to do is to step back from the specific thing you are stuck on, and tell me what you are trying to achieve with your data. The reason is, I don't think your data is structured well, and without that you can't really get an efficient solution. – Owen Hempel Jun 20 '17 at 19:44
  • So, start by trying to explain the purpose of your program: I want to take X inputs, and by some process, return Y outputs. Here, x is your 2 lists, and Y is... I don't know. You touched on it: print monday(1,1). This should return the data in the corner of the Monday table? How did you make the table? What does making the table do? – Owen Hempel Jun 20 '17 at 19:49
  • By the way, I'm not trying to be hard on you, I'm asking you to do what's called "rubber duck debugging", where you explain your problem to a rubber duck. I want to help you with a really good solution, so I need as much info as I can get. – Owen Hempel Jun 20 '17 at 20:39
  • The table itself is a set of constants to determine the percent heat return per hour of the day. These were found using a dissertation. Basically once I have all of these constants lined up, I multiply the whole list against a heat load to get the amount of heat used during that hour of the day. – Justin Taylor Jun 21 '17 at 08:57
  • So would you want all of the constants from hr0, one for each temperature at 5C intervals? Lastly, is your table for each day set up as a list of lists (like a MATLAB matrix)? – Owen Hempel Jun 21 '17 at 10:11
  • Or are you trying to look up the closest value to the value in your temperatures data list? – Owen Hempel Jun 21 '17 at 10:22
  • My tables is set up as a list of lists. For each temperature and hour there should only be one value taken from the table. I edited the table because I don't think I made it clear what the table looks like. I thought I had copied and pasted correctly sorry D: – Justin Taylor Jun 21 '17 at 12:18

1 Answers1

0

Okay, bear with me here, I'm on mobile. I'll try to write up a solution.

I am assuming the following:

  • you have a dictionary called day_data which contains the table of data for each day of the week.
    • you have a dictionary called days which maps 0-6 to a day of the week. 0 is monday, 6 is Sunday.
    • you have a list of temperatures you want something done with
    • you have a time of the day you want to use to pick out the appropriate data from your day_data. You want to do this for each day of the year.

We should only have to iterate once through all 365 days and once through each hour of the day.

heat-load-days={}
for day_index in range(1,365):
  day=Days[day_index%7]
  #day is now the Day of the week.
  data = day_data[day]
  Heat_load =[]
  for hour in range(24):
    #still unsure on how to select which temperature row from the data table.
    Heat_load.append (day_data_selected)
  heat-load-days [day] = Heat_load
Owen Hempel
  • 434
  • 2
  • 8
  • Thank you, I think I understand more what you mean now that I see it though. Thank you! – Justin Taylor Jun 21 '17 at 12:28
  • Awesome. See [this](https://stackoverflow.com/questions/12141150/from-list-of-integers-get-number-closest-to-a-given-value) StackOverflow question on how to find the value in a list closest to the supplied value. It's O(n), which means it'll cost you a bit in terms of time, but it wont be anywhere near as bad as it was before. – Owen Hempel Jun 21 '17 at 21:28