Joe Dingle
Joe Dingle

Reputation: 123

Python grade distribution function, counting and printing the amount of times a grade occurs

I am creating a grade distribution function using the following code:

def distribution(grades):
available_grades = [ 'A+','A','A-','B+','B','B-','C+','C','C-','F']     
fin = open(grades,'r')
gradesList = fin.readline().split(' ')
for c_grade in available_grades:
    if c_grade in gradesList:
        print('students got '+c_grade)

I am using a grades.txt file which includes all of the grades and I need a way to count how many times a grade occurs in that text file and print that number like so:

 distribution('grades.txt')
6 students got A
2 students got A-
3 students got B+
2 students got B
2 students got B-
4 students got C
1 student got C-
2 students got F

but currently it only prints this:

students got A
students got A-
students got B+
students got B
students got B-
students got C
students got C-
students got F

My grades.txt file consists of:

A A- C F C C B- B A A A- B B+ B+ B+ C C- B- A A A F

Upvotes: 0

Views: 1405

Answers (4)

ako
ako

Reputation: 3689

Or, a pandas solution:

fin = open(grades,'r')
grades_given = fin.readline().split()
fin.close()

pd.Series(grades_given).value_counts().reset_index(name='cnt').\
apply(lambda x: "{cnt} students got {grade}".format(cnt=x['cnt'],
      grade=x['index']),axis=1)

0     6 students got A
1     4 students got C
2    3 students got B+
3    2 students got A-
4    2 students got B-
5     2 students got F
6     2 students got B
7    1 students got C-

Upvotes: 1

daniel451
daniel451

Reputation: 11002

You can use the python Counter class for this.

from collections import counter

# creates a new Counter object
c = Counter()

# your file contents
lst_grades = "A A- C F C C B- B A A A- B B+ B+ B+ C C- B- A A A F"

# turn lst_grades to a list containing the grades
lst_grades = lst_grades.split(" ")

# printing lst_grades for a better idea what lst_grades looks like now
# lst_grades
# > ['A', 'A-', 'C', 'F', 'C', 'C', 'B-', 'B', 'A', 'A', 'A-', 'B', 'B+',
#    'B+', 'B+', 'C', 'C-', 'B-', 'A', 'A', 'A', 'F']

# call the update method of the counter c
# update takes an iterable (e.g. a list)
# and counts the values inside this iterable
c.update(lst_grades)

# now our counter contains a dictionary with the counted grads
# c
# > Counter({'A': 6, 'C': 4, 'B+': 3, 'B': 2, 'F': 2,
#            'B-': 2, 'A-': 2, 'C-': 1})

# you can simply access every counted entry
# c["A"]
# > 6

Upvotes: 1

Tom Karzes
Tom Karzes

Reputation: 24062

If you don't want to use collections, then the following should work:

def distribution(grades):
    available_grades = [ 'A+','A','A-','B+','B','B-','C+','C','C-','F']
    grade_dict = {}
    for grade in available_grades:
        grade_dict[grade] = 0

    fin = open(grades,'r')
    gradesList = fin.readline().split()
    fin.close()

    for grade in gradesList:
        grade_dict[grade] += 1

    for grade in available_grades:
        print(str(grade_dict[grade]) + ' students got ' + grade)

Note that I removed the argument to split(), so that it will remove all white space and not just spaces.

Upvotes: 2

BrenBarn
BrenBarn

Reputation: 251428

Use collections.Counter:

def distribution(grades):
    available_grades = ['A+','A','A-','B+','B','B-','C+','C','C-','F']     
    with open(grades, 'r') as fin:
        gradeCounts = collections.Counter(fin.readline().split())
    for grade in available_grades:
        print(gradeCounts[grade], 'students got', grade)

Upvotes: 2

Related Questions