Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I display the largest mean in python code?

How do I display the largest mean in python code?In this code, the average of a number of numbers is calculated. I want the largest average to be displayed

import csv
from statistics import mean

with open("numbers.csv") as f:
    reader = csv.reader(f)
    for row in reader:
        name = row[0]
        these_grades = list()
        for grade in row[1:]:
            these_grades.append(int(grade))
        print("avrage of %s is %f" % (name, mean(these_grades)))

number.csv:

arad,19,19,20,18
sara,17,18,17,15
mahdi,12,13,15,16
saber,14,13,15,15
adel,19,14,17,16

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use the following approach (accumulate average for each name):

import csv
from statistics import mean

with open('number.csv') as f:
    means = []
    reader = csv.reader(f)
    for row in reader:
        name, grades = row[0], map(int, row[1:])
        mean_ = mean(grades)
        means.append(mean_)
        print('average of %s: %f' % (name, mean_))
    print('Max average: {}'.format(max(means)))

average of arad: 19.000000
average of sara: 16.750000
average of mahdi: 14.000000
average of saber: 14.250000
average of adel: 16.500000
Max average: 19
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading