如何将用户输入的内容与字典中的列表相匹配

2024-06-06 10:48:09 发布

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

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']

Tags: oftheinmydictveryprintsoundlist1
2条回答

我有几个建议在这里应该会有所帮助

  • 首先,我会把症状变成一个set,而不是一个list。为什么?集合是无序的,易于比较和操作。你不在乎症状的顺序,你只想看看有多少匹配。正如您将在下面看到的,使用集合更容易做到这一点
  • 接下来,您将依赖于输入的症状与数据集中的症状完全相同。如果你不能保证这一点,你可能想做一些模糊匹配。如果是这种情况,请查找Levenshtein距离并实现某种方法,以获得输入症状和症状集之间的相似性度量,并选择最接近的

我用精确匹配来解决这个问题的代码(我注意到一些拼写错误,我只是保持原样):

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!')

这应该提供更容易的(无序的)匹配,并且应该显示与输入症状数量最多的匹配的任何紊乱。它不处理领带,但如果需要的话,您可以解决这些细节问题

希望这有帮助,快乐编码

您可以使用check = all(item in mydict[i] for item in list1)检查列表中的所有元素是否都在字典的列表中,即使您可以这样做

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)

相关问题 更多 >