Skip to content Skip to sidebar Skip to footer

How Can I Find The Average Of Each Similar Entry In A List Of Tuples?

I have this list of tuples [('Jem', 10), ('Sam', 10), ('Sam', 2), ('Jem', 9), ('Jem', 10)] How do I find the average of the numbers coupled with each name, i.e. the average of all

Solution 1:

There's a couple ways to do this. One is easy, one is pretty.

Easy:

Use a dictionary! It's easy to build a for loop that goes through your tuples and appends the second element to a dictionary, keyed on the first element.

d = {}
tuples = [('Jem', 10), ('Sam', 10), ('Sam', 2), ('Jem', 9), ('Jem', 10)]
for tuple in tuples:
    key,val = tuple
    d.setdefault(key, []).append(val)

Once it's in a dictionary, you can do:

for name, values in d.items():
    print("{name} {avg}".format(name=name, avg=sum(values)/len(values)))

Pretty:

Use itertools.groupby. This only works if your data is sorted by the key you want to group by (in this case, t[0] for each t in tuples) so it's not ideal in this case, but it's a nice way to highlight the function.

from itertools import groupby

tuples = [('Jem', 10), ('Sam', 10), ('Sam', 2), ('Jem', 9), ('Jem', 10)]
tuples.sort(key=lambda tup: tup[0])
# tuples is now [('Jem', 10), ('Jem', 9), ('Jem', 10), ('Sam', 10), ('Sam', 2)]

groups = groupby(tuples, lambda tup: tup[0])

This builds a structure that looks kind of like:

[('Jem', [('Jem', 10), ('Jem', 9), ('Jem', 10)]),
 ('Sam', [('Sam', 10), ('Sam', 2)])]

We can use that to build our names and averages:

for groupname, grouptuples ingroups:
    values= [t[1] for t in groupvalues]
    print("{name} {avg}".format(name=groupname, avg=sum(values)/len(values)))

Solution 2:

Seems like a straight-forward case for collections.defaultdict

from collections import defaultdict
l = [('Jem', 10), ('Sam', 10), ('Sam', 2), ('Jem', 9), ('Jem', 10)]
d = defaultdict(list)
for key, value in l:
    d[key].append(value)

Then calculating the mean

from numpy import mean
forkeyin d:
    print(key, mean(d[key]))

Output

Jem 9.66666666667
Sam 6.0

Solution 3:

You can also use List comprehensions:

l = [('Jem', 10), ('Sam', 10), ('Sam', 2), ('Jem', 9), ('Jem', 10)]

def avg(l):
    returnsum(l)/len(l)

result= [(n, avg([v[1] for v in l if v[0] is n])) for n inset([n[0] for n in l])]
# resultis [('Jem', 9.666666666666666), ('Sam', 6.0)]

Post a Comment for "How Can I Find The Average Of Each Similar Entry In A List Of Tuples?"