简体   繁体   中英

Average based on first element of list of lists python

Input is list of runs for a batsman. It should return country against which batsman has highest average of runs.

I am trying to find highest average so for example when below list is passed to my method it should return "Pakistan".

[
["Pakistan", 23],
["Pakistan", 127],
["India", 3],
["India", 71],
["Australia", 31],
["India", 22],
["Pakistan", 81]
]

I have tried:

create two dictionaries :

total={'Australia': 31, 'India': 96, 'Pakistan': 231}  
division={'Australia': 1, 'India': 2, 'Pakistan': 3} 

thought of dividing values of two dicts and find highest of them.

Is there any other efficient way?

Thanks for the help.

You can use pandas to achieve that, your code would be like:

import pandas as pd
data = [
    ["Pakistan", 23],
    ["Pakistan", 127],
    ["India", 3],
    ["India", 71],
    ["Australia", 31],
    ["India", 22],
    ["Pakistan", 81]
]
df = pd.DataFrame(data, columns=['country', 'count'])
grouped = df.groupby(['country']).mean().reset_index()
highest = list(grouped.max())
print(highest)

Print:

['Pakistan', '77']

Probably could be done with fewer lines of code but this works!!

def average(data):
    highest = {}
    index = 0
    while True:
        for i in data:
            if i[0] in highest:
                highest[i[0]].append(i[1])
            else:
                highest[i[0]] = [i[1]]
        for i in highest:
            highest[i] = sum(highest[i]) / len(highest[i])
        answer = 0
        for i in highest:
            if highest[i] >= answer:
                answer = i
        return answer
print average(data)

You can create a dictionary with country name as the key and a list of country-count and score as value. then you can further modify the same dictionary for calculating avg and use max to print the country with max avg.

here is the code:

>>> a = [
["Pakistan", 23],
["Pakistan", 127],
["India", 3],
["India", 71],
["Australia", 31],
["India", 22],
["Pakistan", 81]
]
>>> 
>>> 
>>> a
[['Pakistan', 23], ['Pakistan', 127], ['India', 3], ['India', 71],         ['Australia', 31], ['India', 22], ['Pakistan', 81]]
>>> d = {}
>>> for l in a:
        if l[0] not in d.keys():
            d.update({l[0]:[1,l[1]]})
        else:
            d[l[0]] = [d[l[0]][0]+1,d[l[0]][1]+l[1]]


>>> #updated list
>>> d
{'Pakistan': [3, 231], 'Australia': [1, 31], 'India': [3, 96]} 
>>> for key,val in d.items():
d[key] = val[1]/val[0]

#Updated dict with average per country
>>> d
{'Pakistan': 77.0, 'Australia': 31.0, 'India': 32.0}

>>> max(d.items())
('Pakistan', 77.0)
>>> 

There could be easier and more pythonic way to do it but, this is where the logic lies.

Thia is an other way to do it:

lst = [
["Pakistan", 23],
["Pakistan", 127],
["India", 3],
["India", 71],
["Australia", 31],
["India", 22],
["Pakistan", 81]
]
tuples = [tuple(i) for i in lst]
newdata = {}
for k,v in tuples:
    newdata.setdefault(k, []).append(v)
result = {k:(sum(v)/len(v)) for k,v in newdata.items()}
a = max(result)
b = max(result.values())
print "The highest average is %s: %s " % (a,b)

Output: The highest average is Pakistan: 77

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