VT_Drew
VT_Drew

Reputation: 372

Get matching values from dictionaries in a list then add and average

I am trying to get matching values of the "week" key from a list of dictionaries. Ultimately what I want to do is take all the week 1's add up the "efficiency" for the week and find the average, then the same for week 2 and so forth.

Note: actual data set is much larger

data = [{"day": 1/2/2015, "efficiency": 35, "week": 1}, 
        {"day": 1/3/2015, "efficiency": 37, "week": 1}, 
        {"day": 1/4/2015, "efficiency": 48, "week": 2},
        {"day": 1/5/2015, "efficiency": 44, "week": 2}, 
       ]

I could do this with something like:

count = 0
stop_point = len(data)
week_total = []
previous_week = False
avg_per_week = []

for item in data:
    if previous_week == False or previous_week == item['week'] and count != stop_point:
        week_total.append(item['efficiency'])
        previous_week = item['week']
        count += 1
    else:
         total = 0
         for item in week_total:
             total += item
         if count == stop_point:
            previous_week = item['week']
         avrg_efficiency = total / len(week_total)
         avg_per_week.append({"week": previous_week, "average": avrg_efficiency})
         week_total = []
         count += 1
         previous_week = item['week']

But I am thinking there has to be a DRY'er way to do this. Thanks for you help.

Upvotes: 0

Views: 56

Answers (2)

jedwards
jedwards

Reputation: 30210

What about simply:

for week in set(elem['week'] for elem in data):
    matches = filter(None, (elem['efficiency'] if elem['week'] == week else None for elem in data))
    total = sum(matches)
    avg   = total / float(len(matches))
    print "Week=%d  Total=%d  Average=%s" % (week, total, avg)

Which outputs

Week=1  Total=72  Average=36.0
Week=2  Total=92  Average=46.0

Upvotes: 1

Reut Sharabani
Reut Sharabani

Reputation: 31339

Create a mapping between a week and it's items usinf defaultdict:

from collections import defaultdict

week_to_items = defaultdict(list)
for item in data:
    week_to_items[item['week']].append(item)

Now do whatever you want using the mapping, which is easier:

for week, week_items in week_to_items.iteritems():
    efficiency_average = float(sum(map(lambda x: x['efficiency'], week_items))) / len(week_items)
    print week, efficiency_average

Upvotes: 1

Related Questions