如何在Python OOP中检查两个元素是否相同?

isr3a4wc  于 2022-12-17  发布在  Python
关注(0)|答案(2)|浏览(117)
class Course:
  def __init__(self, name, classroom, instructor, day, start_time, end_time):
    self.name = name
    self.classroom = classroom
    self.instructor = instructor
    self.day = day
    self.start_time = start_time
    self.end_time = end_time

class Schedule:
  def __init__(self):
    self.courses = []
    program = [["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"]]

  def add_course(self, course):
    self.courses.append(course)  

  def print_schedule(self):
    days = ["Monday","Tuesday","Wednesday","Thursday","Friday"]
    program = [["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"],["9","10","11","12","13","14","15","16","17"]]
    for course in self.courses:
      for j in range(course.start_time-9,course.end_time-8):
        program[days.index(course.day)][j] += f"      {course.name} class from {course.instructor} at {course.classroom}"
    for i in range(len(days)):
        print(days[i],":")
        for k in program[i]:
          print(k)
        
schedule = Schedule()
schedule.add_course(Course("Physics","MED A11","James","Monday",9,11))
schedule.add_course(Course("Logic Design","EEB 4105","Jack","Wednesday",9,10))
schedule.add_course(Course("Logic Design","EEB 4205","Jack","Wednesday",15,17))

schedule.print_schedule()

这里我想创建一个每周的日程表,我想在两个类冲突的时候写一些东西,所以他们的self.day需要是相同的,时间需要相交。
有时候我可以做一些

time = {for i in range(start_time,end_time+1)}
if time1.intersection(time2) != 0:
     #...

但我不知道如何达到2个不同的课程元素在同一时间。也将是伟大的,如果你有任何建议,为这个代码。

nnsrf1az

nnsrf1az1#

你可以在Course类中添加一个方法来检查它是否与另一个Course冲突。当你添加一个课程时,让它循环遍历现有的课程,看看它是否与你的Schedule中的现有课程冲突。

class Course:
    def __init__(self, name, classroom, instructor, day, start_time, end_time):
        self.name = name
        self.classroom = classroom
        self.instructor = instructor
        self.day = day
        self.start_time = start_time
        self.end_time = end_time

    def check_collision(self, other):
        if self.day == other.day:
            if other.start_time < self.start_time < other.end_time:
                return True
            if other.start_time < self.end_time < other.end_time:
                return True
            if self.start_time < other.start_time < self.end_time:
                return True
            if self.start_time < other.end_time < self.end_time:
                return True
        return False

class Schedule:
    def __init__(self):
        self.courses = []
        self.program = [
            ["9","10","11","12","13","14","15","16","17"],
            ["9","10","11","12","13","14","15","16","17"],
            ["9","10","11","12","13","14","15","16","17"],
            ["9","10","11","12","13","14","15","16","17"],
            ["9","10","11","12","13","14","15","16","17"],
        ]

    def add_course(self, course):
        for c in self.courses:
            if course.check_collision(c):
                print(f'New course has collision with course: {c.name} on {c.day}: {c.start_time}-{c.end_time}')
                break
        else:
            self.courses.append(course)

    def print_schedule(self):
        days = ["Monday","Tuesday","Wednesday","Thursday","Friday"]
        program = [x.copy() for x in self.program]
        for course in self.courses:
            for j in range(course.start_time-9,course.end_time-8):
                program[days.index(course.day)][j] += f"      {course.name} class from {course.instructor} at {course.classroom}"
        for i in range(len(days)):
            print(days[i],":")
            for k in program[i]:
                print(k)
h7appiyu

h7appiyu2#

根据你的代码,你可以为类Schedule定义一个比较函数,你可以创建一个Schedule对象来保存很多课程,所以如果你想访问Schedule中的一门课程,你需要执行schedule.courses[i],但是我建议你在课程类中添加下面的函数来解决你的问题。

def compare_course(self, other_course):
my_time = {i for i in range(start_time, end_time+1)}
other_time = {i for i in range(other_course.start_time, other_course.end_time+1)}
if my_time.intersection(other_course.time) != set():
    return "Course Collides"
else:
    return "Course does not collide"

相关问题