Home > Blockchain >  search and replace 2 items in a nested list in python
search and replace 2 items in a nested list in python

Time:09-07

i have a list of until 3 items each item have a list of until 5 items. i need to replace 2 items from another lists. i get only the strings to change. without return anything for example: i have a class School that have a list of Class [size<=3]. and each class have a list of Student [size<=5] i get two "name"s of Students, i need to search them in School by running on all Classes and all Students and swap them. The Student class have a "name" field. especially if their are not in same ClassRoom

class Student:
 def __init__(self, id, name):
   self.name = name
   self.id = id
class ClassRoom:
 def __init__(self):
   self.Students[]
class School:
 def __init__(self):
    self.classes = []
 def swap(self, name1, name2):
   #???


scl.classes = [c1,c3]
s2 = Student(2,"John")
s6 = Student(6, "Ben")
c1.students = [s1,s2,s3]
c3.students = [s1,s4,s6]
scl.search_replace("John", "Ben")

thanks

CodePudding user response:

You need three classes - School, Class and Student. A School has multiple classes. A Class has multiple students.

In order to be able to easily see what's going on it's a good idea to implement __str__ for each class.

Something like this:

class School:
    def __init__(self, name):
        self._name = name  # name of the school
        self._classes = []  # list of classes in the school

    @property
    def classes(self):
        return self._classes

    @property
    def name(self):
        return self._name

    def add(self, clazz):
        assert isinstance(clazz, Class)
        self.classes.append(clazz)

    def search(self, name):
        for c, clazz in enumerate(self.classes):
            if name in clazz.students:
                return c, clazz.students.index(name)
        return -1, -1

    def replace(self, st1, st2):
        c1, s1 = self.search(st1)
        c2, s2 = self.search(st2)

        if s1 >= 0 and s2 >= 0:
            self.classes[c1].students[s1], self.classes[c2].students[s2] = self.classes[c2].students[s2], self.classes[c1].students[s1]


    def __str__(self):
        return f'{self.name}:\n'   '\n'.join(map(str, self.classes))


class Class:
    def __init__(self, name):
        self._name = name  # name of the class
        self._students = []  # list of students in the class

    @property
    def students(self):
        return self._students

    @property
    def name(self):
        return self._name

    def add(self, student):
        assert isinstance(student, Student)
        self.students.append(student)

    def __str__(self):
        return f'{self.name}: '   ', '.join(map(str, self.students))


class Student:
    def __init__(self, name, ident):
        self._name = name  # student's name
        self._ident = ident  # student ID

    @property
    def name(self):
        return self._name
    @name.setter
    def name(self, name):
        self._name = name

    @property
    def ident(self):
        return self._ident

    def __str__(self):
        return f'{self.name} ({self.ident})'
    
    def __eq__(self, other):
        if isinstance(other, str):
            return other == self.name
        if isinstance(other, Student):
            return other.name == self.name
        return False


school = School('Tel Aviv High School')
clazz = Class('Psychology')
school.add(clazz)  # add the Psychology class to the school

student1 = Student('John', 1)
student2 = Student('Ben', 2)
clazz.add(student1)  # add student to the class
clazz.add(student2)

clazz = Class('Chemistry') # add Chemistry class to the school
school.add(clazz)
clazz.add(Student('Haim', 3))  # add student to the class

print(school) # print the school, all of it classes and students within those classes

school.replace('Haim', 'John')

print(school)  # print the school again and note that Haim and John have changed places

Output:

Tel Aviv High School:
Psychology: John (1), Ben (2)
Chemistry: Haim (3)
Tel Aviv High School:
Psychology: Haim (3), Ben (2)
Chemistry: John (1)
  • Related