How can I modify a list that is composed of objects of a class

0

Hello friends I am trying to be able to modify the content of a specific variable, which is inside an object in a list, by means of the index I can obtain the position of the object in the list, but how can I do it to specifically modify the content of one of the variables of such an object, for example: I have the class:

 class Paciente:
        def __init__(self, cod, name, lastname, dni, bornDate):
            self.codigo = cod
            self.nombre = name
            self.apellido = lastname
            self.cedula = dni
            self.FechaNacimiento = bornDate

Then something of what I am trying to do is the part of the code that I try to solve where I ask the user to enter a field of the object that he wants to modify, once he finds the object in the list, I want to modify the variable "Name" of that object that is in position n

if(menu == 3):
        encontrado = False
        ced = input("Ingrese Cedula a Consultar del paciente: ")
        for e in lst:
            if(ced == e.cedula):
                encontrado = True
                ind = lst.index(e)
                break

        if (encontrado):
            mod = int(input("Escriba 1 si desea modificar este elemento: "))
            if (mod == 1):
                objPaciente = Paciente(0,"","","","",)
                objPaciente.nombre = input("ingrese Nuevo Nombre: ")
                lst.insert(e, aquí no sé que hacer?????)

I would appreciate a lot of your help Thanks!

    
asked by Lx Vlxqs 04.06.2018 в 04:11
source

1 answer

1

If you want to modify an object you do not have to create a new one and insert it in the list, simply modify the attribute of the one you have. To get the index it's more efficient if you do:

for índice, paciente in enumerate(lst):
    if cedula == paciente.cedula:
        break

This avoids going through the list twice.

However, you do not even need the index, just use the reference of the object that returns the cycle for :

if menu == 3:
    ced = input("Ingrese Cedula a Consultar del paciente: ")

    obj_paciente = None
    for paciente in lst:
        if ced == paciente.cedula:
            obj_paciente = paciente
            break
    else:
        print("Paciente no encontrado")

    if obj_paciente:
        mod = int(input("Escriba 1 si desea modificar este elemento: "))
        if mod == 1:
            obj_paciente.nombre = input("ingrese Nuevo Nombre: ")

or directly:

if menu == 3:
    ced = input("Ingrese Cedula a Consultar del paciente: ")

    for paciente in lst:
        if ced == paciente.cedula:
            mod = int(input("Escriba 1 si desea modificar este elemento: "))
            if mod == 1:
                paciente.nombre = input("ingrese Nuevo Nombre: ")
            break
    else:
        print("Paciente no encontrado")

For example:

class Paciente:
    def __init__(self, cod, name, lastname, dni, bornDate):
        self.codigo = cod
        self.nombre = name
        self.apellido = lastname
        self.cedula = dni
        self.FechaNacimiento = bornDate

lst = [Paciente(0, "Pedro", "Martínez", "45217458Z", "10/11/1985"),
       Paciente(1, "Luis", "García", "68866866A", "25/04/1975"),
       Paciente(2, "Laura", "Aquilera", "5599664T", "09/07/1990"),
       Paciente(3, "María", "Zamora", "65656566H", "17/03/2005")
      ]


menu = 3

if menu == 3:
    ced = input("Ingrese Cedula a Consultar del paciente: ")

    for paciente in lst:
        if ced == paciente.cedula:
            mod = int(input("Escriba 1 si desea modificar este elemento: "))
            if mod == 1:
                paciente.nombre = input("ingrese Nuevo Nombre: ")
            break
    else:
        print("Paciente no encontrado")
Ingrese Cedula a Consultar del paciente: 68885669K
Paciente no encontrado   

========================================================    

Ingrese Cedula a Consultar del paciente: 68866866A    
Escriba 1 si desea modificar este elemento: 1    
ingrese Nuevo Nombre: Juan    

>>> print(lst[1].nombre, lst[1].apellido, lst[1].cedula)     
Juan García 68866866A
    
answered by 04.06.2018 в 08:39