简体   繁体   English

自定义对象列表

[英]Custom List of Objects

I am trying to create object of my class and append it to the list.我正在尝试将我的 class 和 append 的 object 创建到列表中。 I am using a custom class of Patient which has some properties and method to set the values of properties.我正在使用患者的自定义 class ,它具有一些属性和设置属性值的方法。 So basically i want to store list of patients where each index has each patients record.所以基本上我想存储每个索引都有每个患者记录的患者列表。 When displaying values from List it is not displaying anything only some ID value like`124 Below is my code for class Patients:当显示列表中的值时,它只显示一些 ID 值,例如`124 下面是我的 class 患者代码:

 class Patients:

    def __init__(self):
        self.patient_id = ""
        self.patient_name = ""
        self.patient_age = ""
        self.patient_gender = ""
        self.patient_disease = ""

  def set_patient_values(self):
    //code to set values which user inputs like 
      self.patient_id=int(input("Please enter patient ID : "))


  //
 //appointment.py file :
    
from Patients import Patients

def AddPatient():
    list2=[]
    p1 = Patients()
    p1.set_patient_values()
    list2.append(p1)
    print(p1.patient_id)
    return list2

if __name__ == "__main__":
    list1 = []
    print("Welcome to Hospital System \n")
    Flag=True
    while Flag:
        print("Press 1. To Create Patient Record ")
        print("Press 2. To View Patient Records ")
        x=int(input("Please input from Menu :"))
        if x == 1:
            list1 = AddPatient()

        elif x==2:
            for vertex in list1:
                print(vertex.patient_id)
        else:
            break

Try read and understand this code, it is what do you want i suppose.尝试阅读并理解这段代码,我想这就是你想要的。

class Patients:
    def __init__(self):
        self.patient_id = ""
        self.patient_name = ""
        self.patient_age = ""
        self.patient_gender = ""
        self.patient_disease = ""

    def set_patient_values(self):
        self.patient_id = int(input("Please enter patient ID : "))


def AddPatient():
    pat = Patients()
    pat.set_patient_values()
    return pat


if __name__ == "__main__":
    list_patient = []
    print("Welcome to Hospital System \n")
    while True:
        x = int(input("Press 1. To Create Patient Record\nPress 2. To View Patient Records\n"))
        if x == 1:
            list_patient.append(AddPatient())
        elif x == 2 and list_patient:
            for pat in list_patient:
                print(f'patiend_id = {pat.patient_id} for object {pat}')
        else:
            break
class Patients:
    
    def __init__(self):
        self.patient_id = ""
        self.patient_name = ""
        self.patient_age = ""
        self.patient_gender = ""
        self.patient_disease = ""

    def set_patient_values(self):
        self.patient_id = int(input("Please enter patient ID : "))
        self.patient_name = str(input("Enter patient's Name:"))
        self.patient_age = int(input("Enter patient's age:"))
        self.patient_gender = str(input("Enter patient's gender:"))
        self.patient_disease = str(input("Enter patient's disease:"))

# to test the code on one individual patient : 
# tempPatient = Patients()
# tempPatient.set_patient_values()   
# patientDict[tempPatient.patient_id] = tempPatient.__dict__              

if __name__ == "__main__":
    
    patientDict = {}
    detailDict = {}
    print("Welcome to Hospital System \n")
    Flag=True
    while Flag:
        print("Press 1. To Create Patient Record ")
        print("Press 2. To View Patient Records ")
        x = int(input("Please input from Menu :"))
        if x == 1:
            tempPatient = Patients()
            tempPatient.set_patient_values()
            patientDict[tempPatient.patient_id] = tempPatient.__dict__            
                      
        elif x == 2:
            
            for vertex in patientDict:
                print(patientDict[vertex])
        else:
            break

I've made some changes that might help you:D我做了一些可能对你有帮助的改变:D

I added a dictionary that will contain details of the patients.我添加了一本包含患者详细信息的字典。 You can further try formatting the output code so it's more readable.您可以进一步尝试格式化 output 代码,使其更具可读性。

I hope this answer was helpful:D我希望这个答案有帮助:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM