简体   繁体   中英

find max average from a list python

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

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'

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'

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 !

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM