Suppose I have a sample array that looks like this:
color_values = [{'score': 3, 'color': u'blue'}, {'score': 1, 'color': u'red'}, {'score': 4, 'color': u'green'}, {'score': 4, 'color': u'red'}, {'score': 2, 'color': u'blue'}]
How do I write a code in python for a new array which averages the score of all the same colors? So the new array would look like:
color_values = [{'score': 2.5, 'color': u'blue'}, {'score': 2.5, 'color': u'red'}, {'score': 4, 'color': u'green'}]
The simplest (if not the shortest) way to solve this kind of problem is to build a
dictfrom the key to a list of values, and then aggregate it: