Vivek Anand
Vivek Anand

Reputation: 391

In list of lists, how to find average of values associated with inner lists?

I have a list like this

l=[[Alex,12],[John,14],[Ross,24],[Alex,42],[John,24],[Alex,45]]

how should I process this list that I get a output like this

l=[[Alex,33],[John,19],[Ross,24]]

which is basically the average of scores per each name.

Upvotes: 2

Views: 88

Answers (3)

Ehsan
Ehsan

Reputation: 12397

Use pandas to group by name and calculate mean (l is your list):

import pandas as pd
df = pd.DataFrame(l,columns=['name','value'])
l = df.groupby('name').value.mean().reset_index().values.tolist()

df:

   name  value
0  Alex     12
1  John     14
2  Ross     24
3  Alex     42
4  John     24
5  Alex     45

output:

[['Alex', 33], ['John', 19], ['Ross', 24]]

Upvotes: 1

AdamF
AdamF

Reputation: 2930

lets simplify the problem, by constructing new dict from it, where the keys is the names or the inner lists first element and the value is the average. since keys are unique in python dicts, this become easy. after doing this we will generate a new list from the constructed dict and this will be our answer.

TheOriginalList=[[Alex,12],[John,14],[Ross,24],[Alex,42],[John,24],[Alex,45]] 
aux_dict = {}
for inner_list in TheOriginalList:
    if not aux_dict.get(inner_list[0],None):           #_1_
        aux_dict[inner_list[0]]=[inner_list[1],1]      #_2_
    else:
        aux_dict[inner_list[0]][0]+= inner_list[1]     #_3_
        aux_dict[inner_list[0]][1]+= 1                 #_4_


final_list = []
for k,v in aux_dict.items():                           #_5_
    final_list.append([k,v[0]/v[1]])                   #_6_

explinations

  1. in #1 we are trying to get the key which is the person name, if it already exist in the dict we will get its value which is a list of 2 int items [acumaltive_score , counter] and this will send us to the else to #3. if its not we enter #2
  2. here we add the key (person name to the dict) and set its value to be new list of 2 items [current_score, 1], 1 is the first score. its a counter we need it later for average calculations.
  3. we get here #3, because this person already exist in the dict. so we add its current score to the scores and in #4 we increments the counter by 1.
  4. we explain it (incrementing the counter by 1)
  5. in #5 we iterates over the dict keys and items, so we get in each iteration the key(person name) and the value (list of 2 items, the first item is the total score and the second is the number of the scores).
  6. here in #6 we construct our final list, by appending anew list (again lis of 2 items, in the 0 index the name of the person which is the current key and in index 1 the average which is the v[0]/v[1].

take in mind that this code can raises exceptions in some cases. consider to use try-except

Upvotes: 1

Skander HR
Skander HR

Reputation: 620

l = [['Alex',12],['John',14],['Ross',24],['Alex',42],['John',24],['Alex',45]]

score_dict = {}

for l_score in l:
    name = l_score[0]
    score = l_score[1]
    if name in score_dict.keys():
        score_dict[name].append(score)
        
    else:
        score_dict[name] = [score]

ret_list = []
for k, v in score_dict.items():
    sum_l = sum(v)
    len_l = len(v)
    if len_l > 0:
        avg = float(sum_l)/float(len_l)
    else:
        avg = 0
    ret_list.append([k,avg])
    
print(ret_list)

this should return the following list :

[['Ross', 24.0], ['Alex', 33.0], ['John', 19.0]]

I did not use any package as there were no imports in your code sample. It can be simplified with numpy or pandas

Upvotes: 1

Related Questions