piggy
piggy

Reputation: 115

calculate the average of each sublist separately inside a list of lists

I am really new to Python and I am trying to find average of a list of lists. I have a list of lists of float numbers that indicate the grades of courses per semester and looks like this:

 mylist =  [[[2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0]], [[2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0], [2.67, 2.67, 2.0, 2.0]]]

What I want to do is find the average of each sublist and place it as a sublist again in order to access it easier. For example I want the following:

myaverage= [[[2.335],[2.335],[2.335],...]]]

It is not on purpose the same numbers it just happened at this part of the list that I am showing you. I tried to do this:

for s in mylist:  # for each list
        gpa = sum(s) / len(s)
        allGPA.append(gpa)
        for x in s:  # for each sublist
            x_ = x if type(x) is list else [x]
            myaverage.append(sum(x_) / float(len(x_)))

but I am getting this error:

gpa = sum(s) / len(s) TypeError: unsupported operand type(s) for +: 'int' and 'list'

I cannot understand if my approach is completely wrong or if I am looping wrong through the list.

Upvotes: 0

Views: 1122

Answers (3)

Ali Hassan
Ali Hassan

Reputation: 966

Check this out i have updated my answer, output is as it is you want.**Output**

allGPA = []
myaverage = mylist
c = 0
count = 0
gpa = [0]
for list in mylist:
    for i in range(len(list)):
        gpa[0] = sum(mylist[c][i]) / len(mylist[c][i])
        allGPA.append(gpa)
        myaverage[c][i] = gpa
        print(myaverage[c][i])

    c = c + 1
print(myaverage)

Upvotes: 1

Umar.H
Umar.H

Reputation: 23099

I think it would be prudent to hold your data in some sort of collection, lets use a dictionary and create a readable function to parse your data.

Function

from collections import defaultdict

def return_averages(gpa_lists):
    """ Takes in a list of lists and returns a dictionary of averages.
     the key will be the level of each sublist."""
    gpa_dict = {number_of_list : outer_list for number_of_list, outer_list in enumerate(gpa_lists)}

    gpa_averages = defaultdict(list)
    
    for list_number,lists in gpa_dict.items():
        for each_list in lists:
            gpa_averages[list_number].append(sum(each_list) / len(each_list))
        
    return gpa_averages
   

Usage.

return_averages(mylist)


defaultdict(list,
            {0: [2.335,
              2.335,
              2.335,
              2.335,
              2.335,
              2.335,
              2.335,
              2.335,
              2.335,
              2.335],
             1: [2.335,
              2.335,
              2.335,
              2.335,
              2.335,
              2.335,
              2.335,
              2.335,
              2.335,
              2.335,
              2.335,
              2.335,
              2.335,
              2.335]})

Upvotes: 1

m.k.
m.k.

Reputation: 332

Give this a try:

from statistics import mean

avg = [[ mean(sub_list) for sub_list in list ] for list in mylist]

If the syntax looks a little confusing have a look at list comprehensions

Upvotes: 1

Related Questions