I would like to add an extra key called "centroid"
to this loc1
and loc2
dictionary by using some custom centroid calculation function. location_list
is the list of dictionaries loc1
and loc2
location_list = [loc1,loc2]
loc1 = {
"x_cor" : 10,
"y_cor" : 20}
loc2 = {
"x_cor" : 10,
"y_cor" : 25}
I would like to get the centroid of these two locations and add an extra key "centroid" to both loc1 and loc2.
The function for calculating centroid is as follows:
def get_centroid(self, locations: list):
x, y = [p[0] for p in locations], [p[1] for p in locations]
centroid = [round(sum(x) / len(locations), 2), round(sum(y) / len(locations), 2)]
return centroid
Expected output:
loc1 = {
"x_cor" : 10,
"y_cor" : 20,
"centroid": (6.667 , 15)}
loc2 = {
"x_cor" : 10,
"y_cor" : 25,
"centroid": (6.667 , 15)}
Is there a way I can use this function to add an extra key "centroid" to these dictionaries?