2021年3月4日星期四

Sort a list containing objects that have datetime attributes

I have excel files with data about student test scores, they can take a test up to three times but I need my program to recognize the order in which they took them and the excel file is not in order. I have a Student class that holds a list of all the student's attempts and an Attempt class, that includes the score and date/time the test was taken.

class Student:      def __init__(self, last_name, first_name ):          self.first_name = first_name          self.last_name = last_name            # will hold Attempt objects that consist of score and date/time test taken          self.attempts = []    class Attempt:      def __init__(self, score_as_fraction, datetime_obj):          self.score_as_fraction = score_as_fraction          self.date_and_time_of_attempt = datetime_obj  

So say after reading one excel file one student took the test 3 times so an imaginary student_obj.attempts = [<attempt.Attempt object at 0x105294da0>, <attempt.Attempt object at 0x102431c50>,<attempt.Attempt object at 0x105294f28>] exists but that list is not ordered by time. Is there any way to reorder this attempts list from first attempt to last attempt? I would show what I have tried but I am honestly not sure what to attempt. I think I included all the info necessary as I imagine it's just a method added to the Student class but if you need more please let me know. Thanks for any help.

https://stackoverflow.com/questions/66485238/sort-a-list-containing-objects-that-have-datetime-attributes March 05, 2021 at 08:58AM

没有评论:

发表评论