Python 显示“内存地址不是对象列表”?

Python display "Memory Address not Object List"?

提问人:ni-stor 提问时间:7/19/2023 更新时间:7/19/2023 访问量:25

问:

我正在尝试使用爬山算法输出类调度问题之间的冲突,但是当调用方法“conflict.getConflictBetweenClasses()”时,它在第二列中一直显示内存地址。 这是我打印“冲突表”的代码:

 def display_conflicts(self, schedule):       
       table_conflicts = prettytable.PrettyTable(['Conflict Type', 'Conflict Between Classes'])
       for conflict in schedule.getConflicts():
          table_conflicts.align['Conflict Type'] = "l"  
          table_conflicts.add_row([conflict.getConflictType(), conflict.getConflictBetweenClasses()])
        #  table_conflicts.align = 'l'
       print(table_conflicts)
       return self

这是我的冲突类:

class Conflict:
    class ConflictType(Enum):
        numbOfStudents__ = 1
        InstructorBooking = 2
        RoomBooking = 3

    def __init__(self, conflictType, conflictBetweenClasses):
        self.conflictType = conflictType
        self.conflictBetweenClasses = conflictBetweenClasses

    def getConflictType(self): return self.conflictType.name.split(".")[-1]
    def getConflictBetweenClasses(self): return self.conflictBetweenClasses


    def __str__(self):
        return str(self.conflictType) + " " + str(self.conflictBetweenClasses)

这是我的日程安排课程:

class Schedule:
    classNumb = 0

    def __init__(self, schedule):
        self.conflicts = []
        self.data = schedule
        self.classes = [] * data.getNumberOfClasses()
      #  self.classes = list(self.getClasses())  # Create a copy of the list
        self.classes.extend(self.getClasses())
        self.findAllConflicts()

    def initialize(self):
        for dept in data.getDepts():
            for course in dept.getCourses():
                newClass = Class(self.classNumb, dept, course)
                newClass.setMeetingTime(data.getMeetingTimes()[int(len(data.getMeetingTimes()) * random())])
                newClass.setRoom(data.getRooms()[int(len(data.getRooms()) * random())])
                newClass.setInstructor(course.getInstructors()[int(len(course.getInstructors()) * random())])
                self.classes.append(newClass)
                self.classNumb += 1
        return self

    def findAllConflicts(self):
        conflicts=[]
        for i in self.getClasses():
            if i.getRoom().getSeatingCapacity() < i.getCourse().getMaxNumbOfStudents():
                conflictBetweenClasses = []
                conflictBetweenClasses.append(i)
                conflict = Conflict(Conflict.ConflictType.numbOfStudents__, conflictBetweenClasses)
                self.conflicts.append(conflict)
            for y in self.classes[self.classes.index(i):]:
                if i.getMeetingTime() == y.getMeetingTime() and i.getId() != y.getId():
                    if i.getRoom() == y.getRoom():
                        conflictBetweenClasses = []
                        conflictBetweenClasses.append(i)
                        conflictBetweenClasses.append(y)
                        conflict = Conflict(Conflict.ConflictType.RoomBooking, conflictBetweenClasses)
                        self.conflicts.append(conflict)
                    if i.getInstructor() == y.getInstructor():
                        conflictBetweenClasses=[]
                        conflictBetweenClasses.append(i)
                        conflictBetweenClasses.append(y)
                        conflict = Conflict(Conflict.ConflictType.InstructorBooking, conflictBetweenClasses)
                        self.conflicts.append(conflict)
        return self

    def getClasses(self): return self.classes

    def getData(self): return self.data

    def getConflicts(self): return self.conflicts
  
    def toString(self):
        schedule_list = []
        for cls in self.classes:
            class_info = f"{cls.getDept().getName()}, {cls.getCourse().getNumber()}, {cls.getRoom().getNumber()}, {cls.getInstructor().getId()}, {cls.getMeetingTime().getId()}"
            schedule_list.append(class_info)
        return schedule_list

我多次尝试修改 Conflict 类中的 str(),但仍然输出相同的结果。任何帮助将不胜感激。

python 列表 对象 memory-address

评论


答: 暂无答案