【问题标题】:How to compare an input string to A set of lists and project output as the most matching one? [python]如何将输入字符串与一组列表进行比较,并将项目输出作为最匹配的? [Python]
【发布时间】:2022-12-06 15:25:00
【问题描述】:

作为初学者,我对我正在从事的这个项目有一些疑问 这个项目的总体思路是让用户能够通过症状来检测某种疾病 我希望程序将输入与提供的列表相匹配,并挑选出最佳匹配并打印输出。

这里的问题是,提供疾病症状的列表彼此共享的共同症状很少。所以程序必须评估整个列表集并打印最佳匹配

我尝试了一些东西

import re

input1 = input("Enter Your Symptoms: ".lower())
x = re.split("\s", input1)  #used to seprate the words in the string and create a list out of it

# Symptoms
cadsym = ['chest pain' , 'body pain' , 'falling sick' , 'feeling faint' , 'shortness of breath']
vhdsym = ['swollen ankles' , 'fanting' , 'shortness of breath']
hasym = ['racing heartbeat', 'slow heartbeat', 'chest pain' , 'anxiety', 'sweating']
mhasym = ['cold sweat', 'heartburn', 'sudden dizziness', 'discomfort in joints']
jsym = ['itching', 'abdominal pain', 'weight loss', 'yellow eyes' , 'yellow nails', 'vomiting']
cpsym = ['rashes on skin' , 'fever' , 'sore throat' , 'brown spots' , 'itching']
msym = ['fever', 'runny nose' , 'sneezing' , 'pink eye' , 'skin rash', 'diarrhoea']
dsym = ['Eye pain' , 'fever' , 'muscle pain' , 'nausea' , 'joint pain', 'rash on thigh']
masym = ['pain in muscle' , 'pain in abdomin' , 'Night sweat' , 'shivering', 'fast heart rate' , 'mental confusion']
tcsym = ['chest pain','Night sweats','shortness of breath','blood cough']
disym = ['increase thirst','frequent urination','hunger','blurred vision','slow healing']
pnsym = ['fever','chills','sharp pain in chest','clammy skin']
htsym = ['nose bleeds','dizziness','morning headaches','irregular heart rhythms','vision changes','buzzing in the ears']
emsym = ['lot of mucus','tight chest','whistle sound while breathing']
cysym = ['bluish colour in sikn',' lips','nail beds']
hysym = ['itchy','red and watery eyes','rod of mouth being itchy','runny or blocked nose']
ansym = ['unusual headache','memory loss','slurred speech','forgotten words','trouble in walking','trouble in moving arms','trouble in moving legs']
hcsym = ['anxiety','shortness of breath','headache','daytime sleep even after sleeping a lot at night','daytime sluggishness']
bcsym = ['sleeping difficulty','sore throat','chest pressure','shortness of breath','runny nose']
asym = ['wheezing','anxiety','early awakening','shortness of breath at night','cough','throat irritation']

if x in cadsym:
    print("You Might Have Coronary Artery Disease")
elif x in vhdsym:
    print("You Might Have Vulvar Heart Disease")
elif x in hasym:
    print("You Might Have Heart Arrhythmia ")
elif x in mhasym:
    print("You Might Have Minor Heart Attack")
elif x in jsym:
    print("You Might Have Jaundice")    
elif x in cpsym:
    print("You Might Have Chickenpox")
elif x in msym:
    print("You Might Have Measles")
elif x in dsym:
    print("You Might Have Dengue")
elif x in masym:
    print("You Might Have Malaria")
elif x in tcsym:
    print("You Might Have Tuberculosis")
elif x in disym:
    print("You Might Have Diabetes")
elif x in pnsym:
    print("You Might Have Pneumonia")
elif x in htsym:
    print("You Might Have Hypertension")
elif x in emsym:
    print("You Might Have Emphysema")
elif x in cysym:
    print("You Might Have Cyanosis")
elif x in hysym:
    print("You Might Have Hay Fever")
elif x in ansym:
    print("You Might Have Anoxia")
elif x in hcsym:
    print("You Might Have Hypercapnia")
elif x in bcsym:
    print("You Might Have Bronchitis")
elif x in asym:
    print("You Might Have Asthama")   
else:
    print("Not Registered")

【问题讨论】:

  • 尝试运行for loop on x为每个列表维护一个临时变量。 x in the list 将 temp 变量增加 1,最后检查哪个 temp_variable 具有更多价值和 print 疾病

标签: python project


【解决方案1】:

要将输入字符串与一组列表进行比较并打印最佳匹配,您可以在集合数据类型中使用交集方法。 intersection 方法返回两个集合之间的共同元素,这使您可以确定哪个列表与输入字符串具有最匹配的症状。

下面是一个示例,说明如何使用交集方法找到最佳匹配并打印相应的输出:

重新进口

从用户那里获取输入

input1 = input("请输入您的症状:".lower())

将输入字符串拆分为症状列表

x = re.split("s", input1) #用于分离字符串中的单词并从中创建一个列表

将输入列表转换为集合

x_set = 集合(x)

症状

cadsym = ['胸痛','身体疼痛','生病','感觉头晕','呼吸急促'] vhdsym = ['脚踝肿胀', '发麻', '呼吸急促'] hasym = ['心跳加速', '心跳缓慢', '胸痛', '焦虑', '出汗'] mhasym = ['冷汗', '胃灼热', '突然头晕', '关节不适'] jsym = ['痒', '腹痛', '体重下降', '眼睛发黄', '指甲发黄', '呕吐'] cpsym = ['皮疹'、'发烧'、'喉咙痛'、'褐色斑点'、'发痒'] msym = ['发烧', '流鼻涕', '打喷嚏', '红眼病', '皮疹', '腹泻'] dsym = ['眼痛', '发烧', '肌肉痛', '恶心', '关节痛', '大腿皮疹'] masym = ['肌肉痛','腹痛','盗汗','发抖','心率加快','精神错乱'] tcsym = ['胸痛','盗汗','气短','血咳'] disym = ['增加口渴','尿频','饥饿感','视力模糊','愈合缓慢'] pnsym = ['发烧','发冷','胸部剧痛','皮肤湿冷'] htsym = ['流鼻血','头晕','晨起头痛','心律不齐','视力改变','耳鸣'] emsym = ['很多粘液','胸闷','呼吸时有口哨声'] cysym = ['sikn 中的蓝色','嘴唇','指甲床'] hysym = ['发痒','眼睛发红流泪','嘴巴发痒','流鼻涕或鼻塞'] ansym = ['异常头痛','记忆力减退','口齿不清','忘记单词','走路困难','手臂移动困难','腿移动困难'] hcsym = ['焦虑','呼吸急促','头痛','晚上睡多了还是睡不着','白天精神萎靡'] bcsym = ['睡眠困难','喉咙痛','胸闷','气短','流鼻涕'] asym = ['喘息','焦虑','早醒','夜间呼吸急促','咳嗽','喉咙痛']

【讨论】:

  • 尝试以更好的方式编码格式..!选择所有正文内容并按ctrl+k
【解决方案2】:

由于每个案例的响应都是相同的句子,只是疾病名称发生了变化,为什么不使用键是疾病名称而值是症状列表的字典。

这里更大的挑战可能是许多症状都包含空格,因此完整输入字符串的任何拆分都会阻止您找到匹配项。例如,如果用户输入shortness of breath,那么 x 将包含['shortness', 'of', 'breath'] 有两种可能的解决方法:

  1. 要求用户一次输入一种症状并将所有症状放入列表中
  2. 将症状列表拆分为所有单个单词,但这会导致更多的误报。

    这是我使用集合的解决方案,提示用户输入不同的症状,并将所有疾病数据放入一个常量字典中。

    
    # Use a set for user symptoms to prevent duplicates, and so we can compare
    # against our database easily later.
    user_symptoms = set()
    print("Enter your symptoms one at a time. Leave the entry blank to stop.")
    while True:
        # .lower() needs to be OUTSIDE the input() call,
        #  otherwise it changes the prompt, not the input
        x = input("Enter a symptom: ").lower()
        if len(x):
            user_symptoms.add(x)
        else:
            break
    
    # Dict of diseases with symptoms as sets.
    # We can use sets instead of lists as ordering is not important.
    DIAGNOSES = {
        "coronary artery disease": {'chest pain', 'body pain', 'falling sick', 'feeling faint', 'shortness of breath'},
        "vulvar heart disease":  {'swollen ankles', 'fanting', 'shortness of breath'},
        "heart arrhythmia": {'racing heartbeat', 'slow heartbeat', 'chest pain', 'anxiety', 'sweating'},
        "minor heart attack": {'cold sweat' 'heartburn', 'sudden dizziness', 'discomfort in joints'},
        "jaundice": {'itching', 'abdominal pain', 'weight loss', 'yellow eyes', 'yellow nails', 'vomiting'},
        "chickenpox": {'rashes on skin', 'fever', 'sore throat', 'brown spots', 'itching'},
        "measles": {'fever', 'runny nose', 'sneezing', 'pink eye', 'skin rash', 'diarrhoea'},
        "dengue": {'Eye pain', 'fever', 'muscle pain', 'nausea', 'joint pain', 'rash on thigh'},
        "malaria": {'pain in muscle', 'pain in abdomin', 'Night sweat', 'shivering', 'fast heart rate', 'mental confusion'},
        "tuberculosis": {'chest pain', 'Night sweats', 'shortness of breath', 'blood cough'},
        "diabetes": {'increase thirst', 'frequent urination', 'hunger', 'blurred vision', 'slow healing'},
        "pneumonia": {'fever', 'chills', 'sharp pain in chest', 'clammy skin'},
        "hypertension": {'nose bleeds', 'dizziness', 'morning headaches', 'irregular heart rhythms', 'vision changes', 'buzzing in the ears'},
        "emphysema": {'lot of mucus', 'tight chest', 'whistle sound while breathing'},
        "cyanosis": {'bluish colour in sikn', ' lips', 'nail beds'},
        "hay fever": {'itchy', 'red and watery eyes', 'rod of mouth being itchy', 'runny or blocked nose'},
        "anoxia": {'unusual headache', 'memory loss', 'slurred speech', 'forgotten words', 'trouble in walking', 'trouble in moving arms', 'trouble in moving legs'},
        "hypercapnia": {'anxiety', 'shortness of breath', 'headache', 'daytime sleep even after sleeping a lot at night', 'daytime sluggishness'},
        "bronchitis": {'sleeping difficulty', 'sore throat', 'chest pressure', 'shortness of breath', 'runny nose'},
        "asthma": {'wheezing', 'anxiety', 'early awakening', 'shortness of breath at night', 'cough', 'throat irritation'},
    }
    
    # Find any possible matches using set.intersection(). Put matches in a list.
    matches = []
    for dx, dx_symptoms in DIAGNOSES.items():
        if user_symptoms.intersection(dx_symptoms):
            matches.append(dx)
    
    
    # Now print all the possible matches.
    for match in matches:
        print(f"You might have {match}.")
    else:
        print("No matching symptoms registered.")
    

    示例运行:

    Enter your symptoms one at a time. Leave the entry blank to stop.
    Enter a symptom: chest PAIN
    Enter a symptom: anxiety 
    Enter a symptom: hunger
    Enter a symptom: SORE throat
    Enter a symptom: 
    You might have coronary artery disease.
    You might have heart arrhythmia.
    You might have chickenpox.
    You might have tuberculosis.
    You might have diabetes.
    You might have hypercapnia.
    You might have bronchitis.
    You might have asthma.
    

【讨论】:

    猜你喜欢
    • 2020-05-26
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    相关资源
    最近更新 更多