-1
student_data = [{'id':1, 'Hacker' : 'DOSHI', 'Rank' : 43},
               {'id':2, 'Hacker' : 'JOSHI', 'Rank' : 45},
               {'id':3, 'Hacker' : 'MOSHI', 'Rank' : 41},
               {'id':4, 'Hacker' : 'LOSHI', 'Rank' : 98},
               {'id':5, 'Hacker' : 'AOSHI', 'Rank' : 14}]

1. Here I want to calculate average of 'Rank' ? 2. In other word, I want to get the average of (43,45,41,98,14)?

Trish Kim
  • 29
  • 4

5 Answers5

1

There're lots of ways to do this. You can try this explicit one:

student_data = [{'id':1, 'Hacker' : 'DOSHI', 'Rank' : 43},
               {'id':2, 'Hacker' : 'JOSHI', 'Rank' : 45},
               {'id':3, 'Hacker' : 'MOSHI', 'Rank' : 41},
               {'id':4, 'Hacker' : 'LOSHI', 'Rank' : 98},
               {'id':5, 'Hacker' : 'AOSHI', 'Rank' : 14}]
counter = 0
sumRank = 0

for i in student_data:
    sumRank+=i['Rank']
    counter = counter+1

average = sumRank/counter
print(average)

Also you can try this one liner solution (using list comprehension):

print(sum([i['Rank'] for i in student_data])/len(student_data))
Taohidul Islam
  • 5,246
  • 3
  • 26
  • 39
1

Iterate over your list of dict, and collect the 'Rank' value from each of it. Then compute the average.

>>> _list = [d['Rank'] for d in student_data]
>>> average = sum(_list)/len(_list)
>>> average
48

Or, even better as suggested by @timgeb

average = sum(d['Rank'] for d in student_data)/len(student_data)
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
  • No need for an intermediary list eating memory. Use a generator expression instead (and divide by the length of `student_data`). – timgeb Oct 03 '18 at 14:19
  • You are right, but in this particular case there is very little memory to waste. Plus, I think the OP is still learning the basic skills, so I think this answer may help him approaching this kind of problems. Later on, he will deal with generators. – alec_djinn Oct 03 '18 at 14:23
1

You can use operator.itemgetter:

from operator import itemgetter

res_manual = sum(map(itemgetter('Rank'), student_data)) / len(student_data)  # 48.2

Or, vectorised, you can use a 3rd party library such as Pandas:

import pandas as pd

res_pd = pd.DataFrame(student_data)['Rank'].mean()  # 48.2
jpp
  • 159,742
  • 34
  • 281
  • 339
  • I was trying to find a way using `functools.partial`, but `operator.itemgetter` is just perfect. – Faibbus Oct 03 '18 at 15:35
0

This expression can be directly printed. We can get the sum of all of the values of key Rank by using sum([i['Rank'] for i in student_data]) and we can get the number for our denominator by using len(student_data) which is equal to the number of Rank's we summed, that will give us the average, which we can print

print(sum([i['Rank'] for i in student_data])/len(student_data)) # => 48.2
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
0

You could define a class to help projecting your dataset:

class π(list):
    def __getitem__(self, *args, **kwargs):
        return (a.__getitem__(*args, **kwargs) for a in self)

And then compute the average on the projection:

sum(π(student_data)['Rank'])/len(student_data)
Faibbus
  • 1,115
  • 10
  • 18