user7345804
user7345804

Reputation:

How can I apply a Counter to multiple class instances called in a loop?

I am writing a code to serve as a gradebook for a class I teach. While I have a working code without classes, I am learning and trying to incorporate classes into my code.

I have a class StudentSpecs. There are a lot of things defined in the class (such as name and ID number) but the relevant parts are:

class AstronomyLab154L:

    class StudentSpecs:

        def __init__(self, points=None, grade=None):
            self.points = points
            self.grade = grade

        def __str__(self):
            return "\nTotal Points: %s \nGrade:  %s" %(self.points, self.grade)

        def set_points(self, points):
            self.points = points

        def set_grade(self, grade):
            self.grade = grade

        def get_points(self, points):
            return self.points

        def get_grade(self, grade):
            return self.grade

Using this, my code (available upon request but left out to shorten the post) will read a .csv file (via MS Excel) and create an instance of the StudentSpecs class for each row in the file (each row is a different student, each column represents different assignment scores). So, I can access the total points and letter grade of each student in a loop like so.

## classroom = list of student instances
for student in classroom:
    print(student.points, student.grade)

## example of output
88.0 B
94.0 A
82.5 B-
52.0 F

My goal is to create a histogram of the classroom data, such that the x-axis ticklabels (centered at bins) will store letter-grades and the y-axis will store the number of students with the corresponding letter-grade. I can make the histogram using numpy and matplotlib. But, I don't know how to apply Counter to my class instance. My attempt is below.

from collections import Counter

def display_classroom_specs(classroom):
    ## initialize dict since Counter is a dict
    grade_counts = {}
    ## update dictionary for each student in classroom
    for student in classroom:
        grade_counts += Counter(student.grade) ## y-axis of histogram
    return grade_counts

print(display_classroom_specs(classroom))
## throws an error 
TypeError: unsupported operand type(s) for +=: 'dict' and 'Counter'

I think I should be using the dictionary.update method, though I'm not sure how to apply it in this case.

How can I apply a Counter to my class instances?

Upvotes: 1

Views: 143

Answers (2)

cs95
cs95

Reputation: 403128

You could just pass a generator expression and have Counter do the rest.

c = Counter(student.grade for student in classroom)

Upvotes: 1

user7345804
user7345804

Reputation:

This solution will work. But I am curious to learn more about how to handles class instances.

def display_classroom_specs(classroom):
    grade_counts = []
    for student in classroom:
        grade_counts.append(student.grade)
    grade_counts = Counter(grade_counts)
    print(grade_counts)

display_classroom_specs(classroom)
## example output
Counter({'A': 1}, {'B': 1}, {B-: 1}, {F: 1})

Upvotes: 0

Related Questions