How can I find the average of each similar entry in a list of tuples?

sam cammegh

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 the numbers stored in a tuple with Jem, and then output them? In this example, the output would be:

Jem 9.66666666667
Sam 6
Adam Smith

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 in groups:
    values = [t[1] for t in groupvalues]
    print("{name} {avg}".format(name=groupname, avg=sum(values)/len(values)))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I find the mean 'vote_average' for each actor?

How can I put the average of the values of nested tuples inside a list into a new list?

How can I find the average of a list with multiple cells?

Python: How to find the average on each array in the list?

How to find the average value from a list of tuples that share the same key?

I have a list of strings paired with a number. I want to find the average value for each word that matches to a list of words. How can I do this?

How do I get the average of each similar column value?

how can I find the lowest value of each row and exclude them to find a new average from array

Calculating each average of given list of tuples

How can I count a list of tuples from a list of tuples?

How do I find the average of a column in a list?

How can I transform a list of tuples into a pandas dataframe so that the first value of each tuple represents a column?

How can I iterate the first element of each tuple in a list of tuples through a class method?

How can I sum a list of tuples in Haskell?

How can I convert a dictionary into a list of tuples?

How can I printf a list of tuples?

How can I convert a list of tuples into a mask?

How can I split a list of tuples scala

How can I subtract tuples in a list?

In a list of lists, how can I find the mean of the values for each subindex?

how can i find the average of the observation?

how can I find the average of the sums of a field?

How can I group tuples inside a list that have a similar value of one item and then sort them based on another item?

How can I find similar documents in MongoDB?

how to traverse the given list and find average of second index of tuples and store in a new list in python

How can I find the average of the numbers in a list contained in a string from a text file?

How could I find average of length each category In SQLite

how to print message for average of tuples in a list

How to add a new entry in each of the tuple in a list which is collection of tuples in erlang?