jpdiazpardo's picture
Create dictionaries.py
53f5de2
raw
history blame
1.02 kB
def transform_dict(list_dic):
'''Takes a list of dictionaries and converts the values inside the 'label' key
into a new key and assigns the values of the 'score' key into the new values of the new dictionary.
returns: dictionary'''
new_dicts = {}
for original_dict in list_dic:
key = original_dict['label']
value = original_dict['score']
new_dicts[key] = value
return new_dicts
def calculate_average(list_of_dicts):
'''Calculates the average value across all keys from a list of dictionaries'''
sum_dict = {}
count_dict = {}
# Step 1 and 2
for dictionary in list_of_dicts:
for key, value in dictionary.items():
if key in sum_dict:
sum_dict[key] += value
count_dict[key] += 1
else:
sum_dict[key] = value
count_dict[key] = 1
average_dict = {}
# Step 5
for key in sum_dict:
average_dict[key] = sum_dict[key] / count_dict[key]
return average_dict