如何从方法中的列表中删除变量?

2024-09-28 10:12:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python相当陌生,我想知道如何解决一个问题。我正在做一个基于文本的测试,识别症状并确定你可能患有的疾病。我列出了几个症状清单。然后,我为每种疾病制定了一种方法(重复——我知道),循环检查症状并检查用户输入是否匹配。如果没有,我希望从可能的疾病列表中删除该症状。但是请看,代码可以识别症状是否与第一个问题相匹配,但是程序仍然会在可能性列表中查找该疾病,即使该疾病已被删除。如果疾病不在可能列表中,我尝试使用布尔变量来阻止程序循环。我已经试着使用for循环和可能的列表。我最近的一次尝试是将以前的布尔变量转换成字符串,并以这种方式进行检查。正如我所说,我对Python相当陌生,所以如果这是一个简单的错误,我很抱歉。如果你能帮忙,请帮忙!这个项目周五到期。非常感谢你

下面是代码(如果太长,很抱歉。Python新手,网站新手!):

possibles = [ "InfluenzaA", "InfluenzaB", "InfluenzaC", "CommonCold", "Pneumonia", "StrepThroat", "b", "n", "Croup", "EnterovirusD68", "h", "Herpangina", "PinkEye", "Pertussis"]

InfluenzaA = ["Cough", "Runny nose" "Stuffy nose", "Sneezing", "Sorethroat", "Fever", "Headache", "Bodychills", "Fatigue", "Body aches"]

CommonCold = ["Cough", "Runny nose" "Stuffy nose", "Sneezing", "Sorethroat", "Fever", "Headache", "Bodychills", "Fatigue", "Body aches"]

InfluenzaB = ["Cough", "Stuffy nose", "Sore throat", "Fever", "Headache", "Body chills", "Fatigue", "Muscle aches", "Nausea", "Vomiting"]

InfluenzaC = ["Cough", "Rhinorrhea", "Fever", "Headache", "Muscle pain" ]

CommonCold = ["Cough", "Runny nose", "Stuffy nose", "Congestion", "Sneezing","Sore throat", "Fever", "Headache", "Malaise"]

Pneumonia = ["Cough", "Chest pain", "Fever", "Fatigue", "Loss of appetite", "Body pain", "Shortness of breath", "Fast heartbeat"]

StrepThroat = ["Fever", "Headache", "Throat pain", "Loss of appetite", "Nausea", "Vomiting", "White dots", "Red dots", "Inability or diffuculty swallowing"]

Bronchiolitis = ["Cough", "Runny nose", "Stuffy nose", "Fever", "Shortness of breath", "Wheezing", "Ear pain", "Loss of fluids"]

Norovirus = ["Fever", "Abdominal pain", "Malaise", "Muscle pain", "Diarrhea", "Nausea", "Vomiting"]

Croup = ["Cough", "Runny nose", "Stuffy nose", "Shortness of breath", "Wheezing", "Sore throat", "Fever", "Throat pain", "Fatigue"]

EnterovirusD68 = ["Cough", "Runny nose", "Stuffy nose", "Sneezing", "Wheezing", "Fever", "Body aches"]

HandFootandMouthDisease = ["Sore throat", "Fever", "Headache", "Fatigue","Rash", "Drooling"]

Herpangina = ["Sore throat", "Fever", "Headache", "Fatigue", "Loss of appetite", "Inabilty or diffuculty swallowing", "Mouth blisters or ulcers", "Drooling", "Vomiting"]

PinkEye = ["Red eye", "Eye discharge", "Blurred vision", "Light sensitivity"]

Pertussis = ["Cough", "Runny nose", "Stuffy nose", "Fever", "Fatigue", "Vomiting", "Mucus in the throat"]

stinA = "yes"

stinBe = "yes"

stinC = "yes"

stinPer = "yes"

stinHM = "yes"

stinH = "yes"

stinE = "yes"

stinN = "yes"

stinPE = "yes"

stinCr = "yes"

stinCC = "yes"

stinST = "yes"

def cycleInfluenzaA(sy, stinA):

     for i in range(len(InfluenzaA)):

          if InfluenzaA[i].upper() == sy.upper():

                    return

     possibles.remove("InfluenzaA")

     stinA = "no"

(其余格式相同)

def ask(bod, stinCC, stinCr, stinE, stinA, stinC, stinBe, stinH, stinN, stinPer, stinPE):

  sy = input("Have you experienced any symptoms relation to your " + bod + "\n")

     if stinCC == "yes":

         cycleCold(sy, stinCC)

(其余格式相同)

print("Welcome to this medical database. This is an interactive test to determine what illness a taker may be suffering from. Let's begin.")

ask("chest", stinCC, stinCr, stinE, stinA, stinC, stinBe, stinH, stinN, stinPer, stinPE)

ask("throat", stinCC, stinCr, stinE, stinA, stinC, stinBe, stinH, stinN, stinPer, stinPE)

以下是错误:

Traceback (most recent call last): File "main.py", line 150, in

ask("throat", stinCC, stinCr, stinE, stinA, stinC, stinBe, stinH, stinN, stinPer, stinPE)

File "main.py", line 140, in ask

cycleHerpangina(sy, stinH)

File "main.py", line 100, in cycleHerpangina

possibles.remove("Herpangina")

ValueError: list.remove(x): x not in list


Tags: ofinnosefeveryes疾病painfatigue
2条回答

如果您想收集一名患者的“是”和“否”,因为他们回答是否有症状。并对每种疾病进行计数,您可以像这样循环查看疾病/症状词典(symptom_dict

>>> def interview_patient(patient):
...     for disease in symptom_dict['possibles']:
...         try:
...             for symptom in symptom_dict[disease]:
...                 # answer will be either "yes" or "no".
...                 answer = ask(patient, disease, symptom)
...                 try:
...                     # The patient dict has keys for "yes" and "no".
...                     patient[disease][answer]  += 1
...                     patient[disease]['total'] += 1
...                 except KeyError:
...                     # If the patient didn't have "yes" and "no" keys, 
...                     # create them.
...                     patient[disease] = {'yes': 0, 'no': 0, 'total': 1}
...                     patient[disease][answer]  += 1
...         except KeyError as ke:
...             print(f"There was an error accessing a symptom_dict key: {ke}")
...     return patient
...   

您可以使用一个函数来创建可能患有疾病的患者,并将随机疾病分配给他的“可能性”属性

>>> def get_patient():
...     return { 'likelihood': random.choice(symptom_dict['possibles']) }
...   

为了对场景进行建模和测试,我们可以使用一个简单的函数来回答像患者这样的问题:

>>> def ask(patient, disease ,symptom):
...     if patient['likelihood'] == disease:
...         answer = random.choices(['yes', 'no'], [25, 5])[0]
...     else:
...         answer = random.choices(['yes', 'no'], [5, 25])[0]
...     print('have you experienced %-35s %s' % (symptom, answer))
...     return answer
...   

ask()函数中,答案是加权的,因此当患者被问及与他可能患有的疾病相关的症状时,他更有可能对每个症状回答是。当被问及一种他没有的疾病的症状时,他更可能回答“不”

我将您的疾病/症状数据重新组织到字典中,以便于访问和组织:

>>> symptom_dict = {'possibles': ['InfluenzaA', 'InfluenzaB', 'InfluenzaC', 'CommonCold', 'Pneumonia', 'StrepThroat', 'b', 'n', 'Croup', 'EnterovirusD68', 'h', 'Herpangina', 'PinkEye', 'Pertussis'], 'InfluenzaA': ['Cough', 'Runny noseStuffy nose', 'Sneezing', 'Sorethroat', 'Fever', 'Headache', 'Bodychills', 'Fatigue', 'Body aches'], 'CommonCold': ['Cough', 'Runny nose', 'Stuffy nose', 'Congestion', 'Sneezing', 'Sore throat', 'Fever', 'Headache', 'Malaise'], 'InfluenzaB': ['Cough', 'Stuffy nose', 'Sore throat', 'Fever', 'Headache', 'Body chills', 'Fatigue', 'Muscle aches', 'Nausea', 'Vomiting'], 'InfluenzaC': ['Cough', 'Rhinorrhea', 'Fever', 'Headache', 'Muscle pain'], 'Pneumonia': ['Cough', 'Chest pain', 'Fever', 'Fatigue', 'Loss of appetite', 'Body pain', 'Shortness of breath', 'Fast heartbeat'], 'StrepThroat': ['Fever', 'Headache', 'Throat pain', 'Loss of appetite', 'Nausea', 'Vomiting', 'White dots', 'Red dots', 'Inability or diffuculty swallowing'], 'Bronchiolitis': ['Cough', 'Runny nose', 'Stuffy nose', 'Fever', 'Shortness of breath', 'Wheezing', 'Ear pain', 'Loss of fluids'], 'Norovirus': ['Fever', 'Abdominal pain', 'Malaise', 'Muscle pain', 'Diarrhea', 'Nausea', 'Vomiting'], 'Croup': ['Cough', 'Runny nose', 'Stuffy nose', 'Shortness of breath', 'Wheezing', 'Sore throat', 'Fever', 'Throat pain', 'Fatigue'], 'EnterovirusD68': ['Cough', 'Runny nose', 'Stuffy nose', 'Sneezing', 'Wheezing', 'Fever', 'Body aches'], 'HandFootandMouthDisease': ['Sore throat', 'Fever', 'Headache', 'Fatigue', 'Rash', 'Drooling'], 'Herpangina': ['Sore throat', 'Fever', 'Headache', 'Fatigue', 'Loss of appetite', 'Inabilty or diffuculty swallowing', 'Mouth blisters or ulcers', 'Drooling', 'Vomiting'], 'PinkEye': ['Red eye', 'Eye discharge', 'Blurred vision', 'Light sensitivity'], 'Pertussis': ['Cough', 'Runny nose', 'Stuffy nose', 'Fever', 'Fatigue', 'Vomiting', 'Mucus in the throat']}

运行后,我们得到如下输出:

>>> patient = get_patient()
>>>
>>> interview_patient(patient)

have you experienced Cough                               no
have you experienced Runny noseStuffy nose               yes
have you experienced Sneezing                            yes
have you experienced Sorethroat                          yes
have you experienced Fever                               no
have you experienced Headache                            yes
have you experienced Bodychills                          yes
have you experienced Fatigue                             yes
have you experienced Body aches                          no
have you experienced Cough                               yes
<< list continues on and on >>

在所有的问题之后,我们有一份每种疾病的记录。根据这些计数,你可能会给他们一些概率分数或每种疾病的分数:

>>> patient
{'likelihood': 'StrepThroat', 
'InfluenzaA': {'yes': 4, 'no': 14, 'total': 18}, 
'InfluenzaB': {'yes': 4, 'no': 16, 'total': 20}, 
'InfluenzaC': {'yes': 1, 'no': 9, 'total': 10}, 
'CommonCold': {'yes': 2, 'no': 16, 'total': 18}, 
'Pneumonia': {'yes': 1, 'no': 15, 'total': 16}, 
'StrepThroat': {'yes': 17, 'no': 1, 'total': 18}, 
'Croup': {'yes': 5, 'no': 13, 'total': 18}, 
'EnterovirusD68': {'yes': 2, 'no': 12, 'total': 14}, 
'Herpangina': {'yes': 2, 'no': 16, 'total': 18}, 
'PinkEye': {'yes': 1, 'no': 7, 'total': 8}, 
'Pertussis': {'yes': 3, 'no': 11, 'total': 14}}

把它放在一起作为一个简单的例子,它可能是你可以使用的东西

执行第一个ask()后打印“可能”列表,检查列表中是否仍然存在“Herpangina”

相关问题 更多 >

    热门问题