Reputation: 85
I have a list:
input= [
["Pakistan", 23],
["Pakistan", 127],
["India", 3],
["India", 71],
["Australia", 31],
["India", 22],
["Pakistan", 81]
]
Now i want to filter out only that key value who has highest average . Like in this case output should be "Pakistan":
out = "Pakistan"
Can anybody help
Upvotes: 3
Views: 179
Reputation: 153460
You can use Pandas:
import pandas as pd
l = [
["Pakistan", 23],
["Pakistan", 127],
["India", 3],
["India", 71],
["Australia", 31],
["India", 22],
["Pakistan", 81]
]
pd.DataFrame(l).groupby([0]).mean().idxmax().values[0]
Output:
'Pakistan'
Upvotes: 7
Reputation: 12669
Why don't try in-built max
method , without importing any heavy external module or making it too complex?
input= [
["Pakistan", 23],
["Pakistan", 127],
["India", 3],
["India", 71],
["Australia", 31],
["India", 22],
["Pakistan", 81]
]
track={}
for i in input:
if i[0] not in track:
track[i[0]]=[i[1]]
else:
track[i[0]].append(i[1])
print(max([(sum(j)/len(j),i) for i,j in track.items()]))
output:
(77.0, 'Pakistan')
Upvotes: 1
Reputation: 237
Here is one another variation of implementation.
from collections import defaultdict
import operator
input= [
["Pakistan", 23],
["Pakistan", 127],
["India", 3],
["India", 71],
["Australia", 31],
["India", 22],
["Pakistan", 81]
]
cv_dict=defaultdict(list)
for key, score in input:
cv_dict[key].append(score)
for k,v in cv_dict.items():
cv_dict[k]=sum(v)/len(v)
max(cv_dict, key=cv_dict.get)
Hope it helps !
Upvotes: 2
Reputation: 5805
Another version (with stdlib only):
from __future__ import division
import collections
input= [
["Pakistan", 23],
["Pakistan", 127],
["India", 3],
["India", 71],
["Australia", 31],
["India", 22],
["Pakistan", 81]
]
t = collections.defaultdict(list)
for c,n in input:
t[c].append(n)
max(t, key=lambda c: sum(t[c]) / len(t[c]))
'Pakistan'
Upvotes: 3