0

So I've created this dictionary from a tsv file,

import csv

grades = {}

with open('grades.tsv', newline='') as csv_f:

for row in csv.DictReader(csv_f, delimiter='\t'):

    fullname = row['#fname'] + ' ' + row['lname']

    grades[fullname] = float(row['marks'])

maxgrade = max(grades,key=grades.get)


print(grades)
print(maxgrade) 

csv_f.close()

The tsv file looks like this:

#fname  lname   marks
Alice   Brown   8.5
Francine    Walters 9
Robert  Wilson  7
Evelyn  Stewart 10
Margo   Kazinsky    9.5
Gordon  Rogers  8.5
Stephanie   Hoover  9
Roger   Brosnan 8.7
Francine    Williamson  9
Leonard Runka   8
Rosalind    Samuelson   6.5
Sasha   Levchenko   8
Anastasia   Melnyk  8.5
Charlie Watson  8

After finding the name with the highest grade

  1. I don't know how to manipulate the values to find the names of all the students whose grades are above the mean for the class. (the mean being the sum of the values divided by the number of values).
  2. I don't know how to find the median grade of the class.
  3. And the names of all students whose grades are above the median.
liya77
  • 111
  • 1
  • 8

3 Answers3

2
  1. I don't know how to manipulate the values to find the names of all the students whose grades are above the mean for the class. (the mean being the sum of the values divided by the number of values).

You can use a list comprehension to create a sublist of students that meet a criteria:

above_mean = [(s,g) for s,g in grades.items() if g > mean]
  1. I don't know how to find the median grade of the class.

This is trickier. Sort the student/grade list:

import operator
sorted_by_grade = sorted(grades.items(),key=operator.itemgetter(1))

Then find the middle value. If the length of the list is odd, the middle value index is:

median_index = len(sorted_by_grade) // 2  # Python 3 syntax for integer division

If the length of the list is even, it is the sum of the middle two numbers divided by 2:

half = len(sorted_by_grade) // 2
median = (sorted_by_grade[half-1][1] + sorted_by_grade[half][1]) / 2 # Python 3 float division
  1. And the names of all students whose grades are above the median.

Use a list comprehension on the sorted_by_grade list filtered by grades greater than median similar to above comprehension for mean.

Mark Tolonen
  • 132,868
  • 21
  • 152
  • 208
0

To compute the names whose grades are greater median, you can do:

names=[ii for ii in grades.keys() if grades[ii] > median]

given that you have computed the median, and similar for the mean. Look at this to how to implement the median. To compute the mean do:

mean=float(sum(grades.values()))/len(grades)

Community
  • 1
  • 1
Lord Henry Wotton
  • 1,232
  • 1
  • 10
  • 11
0

Ok, putting all of the answers above, this is what I have:

import csv
import operator

grades = {}

with open('grades.tsv', newline='') as csv_f:

    for row in csv.DictReader(csv_f, delimiter='\t'):

    fullname = row['#fname'] + ' ' + row['lname']

    grades[fullname] = float(row['marks'])

maxgrade = max(grades,key=grades.get)

mean = float(sum(grades.values()))/len(grades)

above_mean = [(student,grade) for student,grade in grades.items() if grade> mean]

sorted_by_grade = sorted(grades.items(),key = operator.itemgetter(1))

median_index = len(sorted_by_grade) // 2 

half = len(sorted_by_grade) // 2

median = (sorted_by_grade[half-1][1] + sorted_by_grade[half][1])/2

names = [ii for ii in grades.keys() if grades[ii]> median]

print(grades)

#print(maxgrade) 

#print(mean)

#print(sorted_by_grade)

#print(above_mean)

print(median)

print(names)

#print(len(grades))

csv_f.close()

Thank you for all of the help!

liya77
  • 111
  • 1
  • 8