File size: 1,022 Bytes
53f5de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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