我有这样的UML图:
School | Teacher | Children
我正在访问python中的类School
,Teache
r和Children
中的数据,如下所示:
for Teacher in School.getTeachers():
print Teacher.getName()
for Children in Teacher.getChildrens():
print Children.getName()
这是我得到的输出:
Mr. Smith A B C D E F Mr. Paul D E F
我想将teacher1
下的每个子名称与教师二下的每个子名称进行比较。
这两个的输出应该是:
A,B,C不属于保罗先生
答案 0 :(得分:0)
使用集合,如下所示:
teachers = list(School.getTeachers())
teacher1 = teachers[0]
teacher2 = teachers[1]
diff = set(teacher1.getChildrens()) - set(teacher2.getChildrens())
print "%s are not under %s" % (", ".join(c.getName() for c in diff), teacher2.getName())
>>> class Student(object):
... def __init__(self, name):
... self.name = name
... def getName(self):
... return self.name
...
>>> class Teacher(object):
... def __init__(self, name, students):
... self.name = name
... self.students = students
... def getChildrens(self):
... return self.students
... def getName(self):
... return self.name
...
>>> class School(object):
... def __init__(self, teachers):
... self.teachers = teachers
... def getTeachers(self):
... return self.teachers
...
>>> students = [Student("A"), Student("B"), Student("C"), Student("D"), Student("E"), Student("F"), ]
>>> s = Teacher("Smith", students)
>>> p = Teacher("Paul", students[3:])
>>> school = School([s, p])
>>>
>>> teachers = list(school.getTeachers())
>>> teacher1 = teachers[0]
>>> teacher2 = teachers[1]
>>>
>>> diff = set(teacher1.getChildrens()) - set(teacher2.getChildrens())
>>> print "%s are not under %s" % (", ".join(c.getName() for c in diff), teacher2.getName())
B, C, A are not under Paul