简体   繁体   中英

How can I match the input entered by the user to the lists in dictionary

print('-----------smart health prediction using data mining------------------- ')
mydict = {
    'malaria': ['fever', 'headache', 'sweats', 'chills', 'vomiting'],
    'anxiety': ['restlessness', 'a sense of dread', 'feeling constantly on edge', 'difficulty concentrating', 'irritability'],
    'asthma': ['wheezing (a whistling sound when you breathe)', 'shortness of breath', 'a tight chest – which may feel like a band is tightening around it', 'Coughing'],
    'hepatitis C': ['shortness of breath', 'angina pectoris', 'anorexia', 'sinus rhythm'],
    'diabetes': ['feeling very thirsty', 'feeling very tired', 'wight loss and loss of muscle in mulk'],
    'migraine': ['increased sensitivity to light and sound', 'vomiting', 'feeling very tierd', 'headache']
}
print(mydict)
j = input("How many symptoms")
x = int(j)
list1 = []
for i in range(x):
    list1.append(input())
print (list1)
# From here I want the list to match the maximum symptoms with the lists in the dictionary
if list1 == mydict['malaria']
    print('true')
else:
    print('false')
mydict['malaria'], mydict['anxiety']

I have a couple of suggestions that should help here.

  • First, I would make the symptoms into a set , rather than a list . Why? Sets are unordered, and made to be easy to compare and manipulate. You don't care what order the symptoms were listed, you just want to see how many match. As you'll see below, this is easier with sets.
  • Next is that you are relying on symptoms being typed in EXACTLY as you have them in your dataset. If you can't guarantee that, you probably want to do some fuzzy matching. If this is the case, look up Levenshtein distance and implement something that gets a measure of similarity between the input symptoms and the sets of symptoms, and chooses the closest.

My code for solving this with exact matches (I noticed a few spelling mistakes, I just left them as-is):

mydict = {
    'malaria': {'fever', 'headache', 'sweats', 'chills', 'vomiting'},
    'anxiety': {'restlessness', 'a sense of dread', 'feeling constantly on edge', 'difficulty concentrating', 'irritability'},
    'asthma': {'wheezing (a whistling sound when you breathe)', 'shortness of breath', 'a tight chest – which may feel like a band is tightening around it', 'Coughing'},
    'hepatitis C': {'shortness of breath', 'angina pectoris', 'anorexia', 'sinus rhythm'},
    'diabetes': {'feeling very thirsty', 'feeling very tired', 'wight loss and loss of muscle in mulk'},
    'migraine': {'increased sensitivity to light and sound', 'vomiting', 'feeling very tierd', 'headache'}
}
print(mydict)
j = input("How many symptoms")
x = int(j)
input_set = set()
for i in range(x):
    input_set.add(input())
print (input_set)
# Lets match the maximum # of symptoms with the sets in the dictionary
max_matches = 0
max_key = None
for disorder, symptoms_set in mydict.items():
    num_matches = len(input_set & symptoms_set) # find the length of the intersection!
    if num_matches > max_matches:
        max_matches = num_matches
        max_key = disorder
print(f'Disorder: {max_key} was best match, with {max_matches} matches!')

This should provide easier (unordered) matching, and should display whatever disorder matched the greatest number of input symptoms. It does not handle ties, but that's details you can work out if desired.

Hope this helps, Happy Coding!

you can use check = all(item in mydict[i] for item in list1) to check if all the elements of your list are in the list of the dictionary, even you can do this

print('-----------smart health prediction using data mining------------------- ')
mydict = {
    'malaria': ['fever', 'headache', 'sweats', 'chills', 'vomiting'],
    'anxiety': ['restlessness', 'a sense of dread', 'feeling constantly on edge', 'difficulty concentrating', 'irritability'],
    'asthma': ['wheezing (a whistling sound when you breathe)', 'shortness of breath', 'a tight chest – which may feel like a band is tightening around it', 'Coughing'],
    'hepatitis C': ['shortness of breath', 'angina pectoris', 'anorexia', 'sinus rhythm'],
    'diabetes': ['feeling very thirsty', 'feeling very tired', 'wight loss and loss of muscle in mulk'],
    'migraine': ['increased sensitivity to light and sound', 'vomiting', 'feeling very tierd', 'headache']
}
print(mydict)
j = input("How many symptoms")
x = int(j)

list1 = []

for i in range(x):
    list1.append(input("Type your "+str(i+1)+" symptom"))
print (list1)

for i in mydict:
    check =  all(item in mydict[i] for item in list1)
    if(check):
        print("you could have "+i)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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