Home > Enterprise >  Average the values of a list of dictionaries
Average the values of a list of dictionaries

Time:03-02

I have the following list of dictionaries. Each dictionary has a "Point" and a "Value" and goes from 1 to 10, for each series of points.

My_list = [{"Point": 1, "Value": 40}, {"Point": 2, "Value": 40}, {"Point": 3, "Value": 40}, \
{"Point": 4, "Value": 40}, {"Point": 5, "Value": 40}, {"Point": 6, "Value": 40}, \
{"Point": 7, "Value": 40}, {"Point": 8, "Value": 40}, {"Point": 9, "Value": 0},{"Point": 10, "Value": 250},\
    {"Point": 1, "Value": 40}, {"Point": 2, "Value": 40}, {"Point": 3, "Value": 40}, \
{"Point": 4, "Value": 40}, {"Point": 5, "Value": 40}, {"Point": 6, "Value": 40}, \
{"Point": 7, "Value": 40}, {"Point": 8, "Value": 40}, {"Point": 9, "Value": 0},{"Point": 10, "Value": 250},\
    {"Point": 1, "Value": 40}, {"Point": 2, "Value": 40}, {"Point": 3, "Value": 40}, \
{"Point": 4, "Value": 40}, {"Point": 5, "Value": 40}, {"Point": 6, "Value": 40}, \
{"Point": 7, "Value": 40}, {"Point": 8, "Value": 40}, {"Point": 9, "Value": 0},{"Point": 10, "Value": 250}]

I would like to find the average 'Value' for every 2 'Point', without messing with the 'Value' of the next series. I want to implement a solution without using numpy or pandas and I have done the following.

every2 = []
counter = 2
temp = []
for point in My_list:
    if counter > 0:
        temp.append(point["Value"])
    else:
        p = point
        p["Value"] = sum(temp)/len(temp)
        every2.append(point)
        # reset the counter after every 2 point
        counter = 2
        temp = []
        # temp.append(point["Value"])
    counter -= 1

print(every2)

The result I am getting is:

[{'Point': 3, 'Value': 40.0}, {'Point': 5, 'Value': 40.0}, {'Point': 7, 'Value': 40.0}, {'Point': 9, 'Value': 40.0},  
 {'Point': 1, 'Value': 250.0}, {'Point': 3, 'Value': 40.0}, {'Point': 5, 'Value': 40.0}, {'Point': 7, 'Value': 40.0},  
 {'Point': 9, 'Value': 40.0}, {'Point': 1, 'Value': 250.0}, {'Point': 3, 'Value': 40.0}, {'Point': 5, 'Value': 40.0},  
{'Point': 7, 'Value': 40.0}, {'Point': 9, 'Value': 40.0}]

However I am missing the first 'Point', as the 'Point' of the first series starts from 3 instead of 1 and as a consequence the 'Point' 9 has a value of 40 instead of 125.

So what I want should look like this:

[{'Point': 1, 'Value': 40.0}, {'Point': 3, 'Value': 40.0}, {'Point': 5, 'Value': 40.0}, {'Point': 7, 'Value': 40.0},   
 {'Point': 9, 'Value': 125.0}, {'Point': 1, 'Value': 40.0}, {'Point': 3, 'Value': 40.0}, {'Point': 5, 'Value': 40.0},  
 {'Point': 7, 'Value': 40.0}, {'Point': 9, 'Value': 125.0}, {'Point': 1, 'Value': 40.0}, {'Point': 3, 'Value': 40.0},  
{'Point': 5, 'Value': 40.0}, {'Point': 7, 'Value': 40.0}, {'Point': 9, 'Value': 125.0}]

CodePudding user response:

You can add a step argument to range() that will allow you to iterate over the list in steps of 2. Then, get both elements you want to use, create a new element using the values, and append that to your result list.

result_list = []

n_step = 2           # chunk size is 2

for i in range(0, len(My_list), n_step):
    # Get all elements in this chunk
    elems = My_list[i:i n_step] 

    # Find the average of the Value key in elems
    avg = sum(item['Value'] for item in elems) / len(elems) 

    # Point key from the first element; Value key from average
    new_item = {"Point": elems[0]["Point"], "Value": avg}
    result_list.append(new_item)

Which gives:

[{'Point': 1, 'Value': 40.0},
 {'Point': 3, 'Value': 40.0},
 {'Point': 5, 'Value': 40.0},
 {'Point': 7, 'Value': 40.0},
 {'Point': 9, 'Value': 125.0},
 {'Point': 1, 'Value': 40.0},
 {'Point': 3, 'Value': 40.0},
 {'Point': 5, 'Value': 40.0},
 {'Point': 7, 'Value': 40.0},
 {'Point': 9, 'Value': 125.0},
 {'Point': 1, 'Value': 40.0},
 {'Point': 3, 'Value': 40.0},
 {'Point': 5, 'Value': 40.0},
 {'Point': 7, 'Value': 40.0},
 {'Point': 9, 'Value': 125.0}]

CodePudding user response:

You can also use list comprehension

res = [{**data[n], **{'Value': sum(v['Value']/2 for v in data[n: n 2])}} for n in range(0, len(data), 2)]
print(res)

Output:

[{'Point': 1, 'Value': 40.0},
 {'Point': 3, 'Value': 40.0},
 {'Point': 5, 'Value': 40.0},
 {'Point': 7, 'Value': 40.0},
 {'Point': 9, 'Value': 125.0},
 {'Point': 1, 'Value': 40.0},
 {'Point': 3, 'Value': 40.0},
 {'Point': 5, 'Value': 40.0},
 {'Point': 7, 'Value': 40.0},
 {'Point': 9, 'Value': 125.0},
 {'Point': 1, 'Value': 40.0},
 {'Point': 3, 'Value': 40.0},
 {'Point': 5, 'Value': 40.0},
 {'Point': 7, 'Value': 40.0},
 {'Point': 9, 'Value': 125.0}]

For python 3.9

[data[n] | {'Value': sum(v['Value']/2 for v in data[n: n 2])} for n in range(0, len(data), 2)]
  • Related