Search code examples
pythonlistdataframedictionarypython-itertools

Comparing key-values in a dictionary as electrical usage per day


I have list of list that's recording the electrical usage for a particular building. The sampling is done every 30 minutes. I'm using a dictionary to group the samples by day/month, I figured after that I could take the max value for each day and subtract the following - previous day, so I can get the total for that day. Where the total is kilowatt-hour.

One of the issues I'm having is accounting for the first max value of the first day/month, because I have nothing to compare that with because that was the first max sample? If there's a better approach please tell? The way I was planning to handle that is during that time just find the difference between min max until I get enough data.

I have created a list of list as an example. The first column will be the month and second recorded value.

The following shows the dictionary grouped by month and then finding the max value. You can see that once the max values and difference are found that month 1 will not be accounted for? The result I should get is {1:4, 2:10, 3:25} which is total by month and then {1:39} for the year?

dict1 = {1: [1, 3, 4, 5], 2: [7, 8, 9, 10, 15], 3: [20, 25, 40]}
dict2 = {1: 5, 2: 15, 3: 40} 

Here is the code so far, I commented out the operations to get the total for the year and by month because my approaches weren't working?

list1 =     [[1, 1],
             [1, 1],
             [1, 0],
             [1, 3],
             [1, 4],
             [1, 5],  # max
             [2, 7],
             [2, 8],
             [2, 9],
             [2, 10],
             [2, 15],  # max
             [3, 20],
             [3, 25],
             [3, 40]]  # max

    # using min/max of each day/month I'll miss the usage between the last value for the 
    #  day/month and
    #  first value of next day/month.
    #  ex: day 1: 5 - 1 = 4 , day 2: = 9 - 7 = 2, day 3: 40 - 12 = 28
    #  missed usage from  5 to 7 and 9 to 12

    #  taking the difference between max values of each day/month
    #  ex: day 1: = 5, day 2: = 9, day 3 = 40  day 1 = 4, day 2 = 31
    #
    # 
    dict1 = {}
    dict2 = {}
    dict3 = {}

    for i, j in list1:
        if j > 0:  # Samples will always be greater than zero so eliminate any sample that's not.
            dict1.setdefault(i, []).append(j)  # Group all the samples by day/month.
            dict1[i] = sorted(list(set(dict1[i])))  # Check for duplicate samples
            max_ = max(dict1[i])
            dict2[i] = max_

            

    print(dict1)
    print(dict2)

Solution

  • You can do what you need using dictionary comprehensions:

    1. Read list1 into dict1:
    list1 = [[1, 1], [1, 1], [1, 0], [1, 3], [1, 4], [1, 5],  
             [2, 7], [2, 8], [2, 9], [2, 10], [2, 15],  
             [4, 20], [4, 25], [4, 40]]
    months = sorted(set(l[0] for l in list1))
    dict1 = {m: sorted(set(l[1] for l in list1 if l[0]==m and l[1]>0)) for m in months}
    >>> dict1
    {1: [1, 3, 4, 5], 2: [7, 8, 9, 10, 15], 4: [20, 25, 40]}
    
    1. Manipulate dict1 to get the differences:

    You can simply use indices -1 and 0 for the maximum and minimum values as the lists are sorted for each date.

    dict2 = {m: dict1[m][-1]-dict1[m-1][-1] if m-1 in dict1 and dict1[m][0]>=dict1[m-1][-1] else dict1[m][-1]-dict1[m][0] for i, m in enumerate(months)}
    >>> dict2
    {1: 4, 2: 10, 4: 20}
    
    1. Get the total for the year using dict2:
    dict3 = {1: sum(dict2.values())}
    >>> dict3
    {1: 34}