I have 13 lists and a dictionary that changes based on the user inputs, the dictionary selects lists from the 13 lists their names are key, and their values are the values of the lists, it changes based on the user inputs so it might have 1,2,3,4,5,.... lists but it is impossible to be 13 max is 11 or 9 I think,
what I want is to change this dictionary into sets or lists with different names than the ones I predefiend these lists names are (key random number or something) and have the same value as a dictionary and I can't use the name of the key because it is random and changes based on the user inputs that's why I was trying to somehow index it or change it to list or sets to work with it
Let me explain in detail:
Alright so I have the following Functions
def loopD(user,dis):
count = 0
for i in range(len(user)):
if dis.count(user[i]):
count = 1
return count
this code takes two lists which are "user" and "dis" it looks for the common elements and returns the count based on the number of the common elements. and this function
def loopA(user,**kwargs):
all_values = args.values()
max_value = max(all_values)
final = {key: val for key, val in args.items() if val == max_value}
commonlist = list(final.keys())
commondict = {}
for i in range(len(commonlist)):
commondict[commonlist[i]] = eval(commonlist[i])
The "user" is the same user list from before the "kwargs" are bunch of other list names from the 13 with the common elements with it and the users list like "anyname = loopD(user,dis)"
so the dictionary is created with names and common elements from the user's list, the names of the list are like 13 each one with a bunch of elements that they have at least 5-6 elements in common for example
list A has 3 Common Elements with list B
and 5 Common Elements with list C
but list B has 2 Common Elements with list C which may or may not be included in A
so this code in the function
all_values = args.values()
max_value = max(all_values)
final = {key: val for key, val in args.items() if val == max_value}
gets the maximum Value of common elements and then filter the dictionary so it only contains the lists with the Greatest common elements now the rest of the code is me trying to get the key of the elements in the dictionary as a name of a list or a set and value as the value of the set or list which is this
commonlist = list(final.keys())
commondict = {}
for i in range(len(commonlist)):
commondict[commonlist[i]] = eval(commonlist[i])
why am I doing this?
I want to use the first function to filter these lists that have the greatest common elements because each list has a Unique element that doesn't exist in the other lists
so if the user for example entered 4 elements and one of these elements is the unique element then it will print the name of the list that has this unique element
I doing this scenario because the user can input 5 elements 4 in a list including the unique element and the last one is just a common one that's is not in that list and that is the reason why I get 4-5 lists that has the same value of common elements.
For Example:
lista = [1,2,3,5,"BANANAS"]
listb = [1,2,4,5,6,"APPLE"]
listc = [1,2,3,4,5,"BLUE"]
listd = [1,2,5,"RED"]
liste = [4,5,"YELLOW"]
user = [1,2,4,5,"BANANAS"]
# code output right now
kwargs= {'lista' : 4, 'listb' : 4, 'listc' : 4 , 'listd' : 3, 'liste' : 2}
# after functions
final = {'lista' : 4, 'listb' : 4, 'listc' : 4}
commondict = {'lista' : [1,2,3,5,"BANANAS"]
, 'listb' : [1,2,4,5,6,"APPLE"], 'listc' : [1,2,3,4,5,"BLUE"]}
# What I am trying to do here is to get from this commondict are 3 variables as lists or
# Sets then search for the common elements between each one of them then REMOVE them So I
# Get the 3 Variables like this
lista = ["BANANAS"]
listb = [6,"APPLE"]
listc = ["BLUE"]
# And then compare them to the user [1,2,4,5,"BANANAS"] looking for the common element
output = 'lista'
#because it is the only one that has the unique element in user
and this is where I couldn't continue the function and I can't use the key of the dictionary because I don't know it in the first place because the user can change the elements of his list and it will give me a whole new dictionary
if you are interested here is the full script
cystic_fibrosis = ['Coughing with or without mucus','Difficulty Breathing','Wheezing','Diarrhea'
,'Recurring chest infections','Damage to the airways (bronchiectasis)'
,'Difficulty putting on weight and growing','Yellowing of the skin'
,'The whites of the eyes (jaundice)','Constipation or large, smelly poo'
,'a bowel obstruction in newborn babies']
asthma = ['Coughing with or without mucus','Difficulty Breathing','Tiredness and fatigue'
,'Wheezing','Rapid heartbeat','fast breath','change in the color of the fingers','Blue lips']
lung_cancer = ['Coughing with or without mucus','Difficulty Breathing','Tiredness and fatigue'
,'Chest pain','Wheezing','Weight loss','Coughing up blood']
covid19 = ['Coughing with or without mucus','Difficulty Breathing','Tiredness and fatigue','Chest pain'
,'Headaches','Diarrhea','change in the color of the fingers','Fever','Loss of sense of taste or smell'
,'Sore throat','body aches and pains','change in the color of the toes'
,'Eye redness or irritation Serious symptom','Loss of speech, movement, or confusion']
emphysema = ['Coughing with or without mucus','Wheezing','Tightness in your chest'
,'Whistling / squeaky sound when you breathe']
Bronchitis = ['Coughing with or without mucus','Tiredness and fatigue','Chest pain','Mild headache'
,'body aches and pains','Sore throat']
pneumothorax = ['Chest pain','Rapid heartbeat','fast breath','Blue lips','Tightness in your chest'
,'Painful breathing','Confusion or Dizziness','Loss of consciousness or coma']
pleural_effusion = ['Coughing with or without mucus','Difficulty Breathing','Chest pain','Painful breathing']
empyema = ['Coughing with or without mucus','Difficulty Breathing','Tiredness and fatigue','Chest pain'
,'Weight loss','Headaches','Loss of appetite','Sweating','Fever','Decreased breath sounds'
,'A Case pneumonia that does not improve','A crackling sound from the chest']
hemothorax = ['Difficulty Breathing','Tiredness and fatigue','Chest pain','Rapid heartbeat','fast breath'
,'Anxiety','Pale, Cool and Clammy skin','High fever over 100°F (38°C)','Low blood pressure']
tuberculosis = ['Coughing with or without mucus','Tiredness and fatigue','Weight loss','Loss of appetite'
,'Sweating','High temperature','Swellings in the neck']
Pulmonary_fibrosis = ['Coughing with or without mucus','Difficulty Breathing','Tiredness and fatigue'
,'Weight loss','Aching muscles and joints'
,'Widening and rounding of the tips of the fingers or toes(Clubbing)']
pneumonia = ['Coughing with or without mucus','Difficulty Breathing','Tiredness and fatigue','Chest pain'
,'Rapid heartbeat','Headaches','Loss of appetite','Sweating','High temperature','Coughing up blood'
,'Aching muscles and joints','Feeling generally unwell','Shivering']
diseases = ['Coughing with or without mucus','Difficulty Breathing','Tiredness and fatigue','Chest pain','High fever over 100°F (38°C)']
def loopa(user,**args):
all_values = args.values()
max_value = max(all_values)
final = {key: val for key, val in args.items() if val == max_value}
commonlist = list(final.keys())
commondict = {}
for i in range(len(commonlist)):
commondict[commonlist[i]] = eval(commonlist[i])
def loopd(user,dis):
count = 0
for i in range(len(user)):
if dis.count(user[i]):
count = 1
return count
loopa(diseases,cystic_fibrosis= loopd(diseases,cystic_fibrosis),asthma= loopd(diseases,asthma)
,lung_cancer= loopd(diseases,lung_cancer),covid19= loopd(diseases,covid19)
,emphysema= loopd(diseases,emphysema),Bronchitis= loopd(diseases,Bronchitis)
,pneumothorax= loopd(diseases,pneumothorax),pleural_effusion= loopd(diseases,pleural_effusion)
,empyema= loopd(diseases,empyema),hemothorax= loopd(diseases,hemothorax)
,tuberculosis= loopd(diseases,tuberculosis),Pulmonary_fibrosis= loopd(diseases,Pulmonary_fibrosis)
,pneumonia=loopd(diseases,pneumonia))
CodePudding user response:
Your code has several issues:
- you define each disease as a separate variable, which means you need to refer to them by name individually later; a more suitable data structure would be to put them all in a single dictionaries, with the lists of symptoms as the values and the names of the diseases as keys.
- you count how often a symptom is mentioned in each disease, but I can only assume no symptom is mentioned twice, so that's always 0 or 1?
- you use
eval()
to evaluate the name of a keyword parameter name, just because it happens to line up with what you named the disease variables; this is extremely sensitive to mistakes, one typo in either and it wouldn't work; what's worse, if your disease name happens to mean something else in Python, that would now be evaluated -eval()
is evil in most cases, avoid it. - your code calls
loopa
, but nothing is returned, so nothing ends up happening - the use of upper and lower case in both disease names and descriptions isn't very consistent, so it will be hard for anyone to enter the exact descriptions you did (including the correct case)
Here's your code again, but without the issues mentioned above:
import pprint
diseases_with_symptoms = {
'cystic_fibrosis': [
'Coughing with or without mucus', 'Difficulty Breathing', 'Wheezing', 'Diarrhea'
, 'Recurring chest infections', 'Damage to the airways (bronchiectasis)'
, 'Difficulty putting on weight and growing', 'Yellowing of the skin'
, 'The whites of the eyes (jaundice)', 'Constipation or large, smelly poo'
, 'a bowel obstruction in newborn babies'],
'asthma': [
'Coughing with or without mucus', 'Difficulty Breathing', 'Tiredness and fatigue'
, 'Wheezing', 'Rapid heartbeat', 'fast breath', 'change in the color of the fingers'
, 'Blue lips'],
'lung_cancer': [
'Coughing with or without mucus', 'Difficulty Breathing', 'Tiredness and fatigue'
, 'Chest pain', 'Wheezing', 'Weight loss', 'Coughing up blood'],
'covid19': [
'Coughing with or without mucus', 'Difficulty Breathing', 'Tiredness and fatigue'
, 'Chest pain', 'Headaches', 'Diarrhea', 'change in the color of the fingers', 'Fever'
, 'Loss of sense of taste or smell', 'Sore throat', 'body aches and pains'
, 'change in the color of the toes', 'Eye redness or irritation Serious symptom'
, 'Loss of speech, movement, or confusion'],
'emphysema': [
'Coughing with or without mucus', 'Wheezing', 'Tightness in your chest'
, 'Whistling / squeaky sound when you breathe'],
'Bronchitis': [
'Coughing with or without mucus', 'Tiredness and fatigue', 'Chest pain',
'Mild headache'
, 'body aches and pains', 'Sore throat'],
'pneumothorax': [
'Chest pain', 'Rapid heartbeat', 'fast breath', 'Blue lips', 'Tightness in your chest'
, 'Painful breathing', 'Confusion or Dizziness', 'Loss of consciousness or coma'],
'pleural_effusion': [
'Coughing with or without mucus', 'Difficulty Breathing', 'Chest pain'
, 'Painful breathing'],
'empyema': [
'Coughing with or without mucus', 'Difficulty Breathing', 'Tiredness and fatigue'
, 'Chest pain', 'Weight loss', 'Headaches', 'Loss of appetite', 'Sweating', 'Fever'
, 'Decreased breath sounds', 'A Case pneumonia that does not improve'
, 'A crackling sound from the chest'],
'hemothorax': [
'Difficulty Breathing', 'Tiredness and fatigue', 'Chest pain', 'Rapid heartbeat'
, 'fast breath', 'Anxiety', 'Pale, Cool and Clammy skin', 'High fever over 100°F (38°C)'
, 'Low blood pressure'],
'tuberculosis': [
'Coughing with or without mucus', 'Tiredness and fatigue', 'Weight loss'
, 'Loss of appetite', 'Sweating', 'High temperature', 'Swellings in the neck'],
'Pulmonary_fibrosis': [
'Coughing with or without mucus', 'Difficulty Breathing', 'Tiredness and fatigue'
, 'Weight loss', 'Aching muscles and joints'
, 'Widening and rounding of the tips of the fingers or toes(Clubbing)'],
'pneumonia': [
'Coughing with or without mucus', 'Difficulty Breathing', 'Tiredness and fatigue'
, 'Chest pain', 'Rapid heartbeat', 'Headaches', 'Loss of appetite', 'Sweating'
, 'High temperature', 'Coughing up blood', 'Aching muscles and joints'
, 'Feeling generally unwell', 'Shivering']
}
patient_symptoms = [
'Coughing with or without mucus', 'Difficulty Breathing', 'Tiredness and fatigue'
, 'Chest pain', 'High fever over 100°F (38°C)']
def best_disease_matches(symptoms):
# you could still do this with count, but this seems simpler
symptom_counts = {
disease: sum(
1 if symptom.lower() in map(lambda s: s.lower(), disease_symptoms)
else 0 for symptom in symptoms
)
for disease, disease_symptoms in diseases_with_symptoms.items()
}
max_count = max(symptom_counts.values())
return [disease for disease, c in symptom_counts.items() if c == max_count]
patient_diseases = {disease: (diseases_with_symptoms[disease])
for disease in best_disease_matches(patient_symptoms)}
pprint.pprint(patient_diseases)
The only reason for the pprint
is to make the output a bit more readable:
{'covid19': ['Coughing with or without mucus',
'Difficulty Breathing',
'Tiredness and fatigue',
'Chest pain',
'Headaches',
'Diarrhea',
'change in the color of the fingers',
'Fever',
'Loss of sense of taste or smell',
'Sore throat',
'body aches and pains',
'change in the color of the toes',
'Eye redness or irritation Serious symptom',
'Loss of speech, movement, or confusion'],
'empyema': ['Coughing with or without mucus',
'Difficulty Breathing',
'Tiredness and fatigue',
'Chest pain',
'Weight loss',
'Headaches',
'Loss of appetite',
'Sweating',
'Fever',
'Decreased breath sounds',
'A Case pneumonia that does not improve',
'A crackling sound from the chest'],
'hemothorax': ['Difficulty Breathing',
'Tiredness and fatigue',
'Chest pain',
'Rapid heartbeat',
'fast breath',
'Anxiety',
'Pale, Cool and Clammy skin',
'High fever over 100°F (38°C)',
'Low blood pressure'],
'lung_cancer': ['Coughing with or without mucus',
'Difficulty Breathing',
'Tiredness and fatigue',
'Chest pain',
'Wheezing',
'Weight loss',
'Coughing up blood'],
'pneumonia': ['Coughing with or without mucus',
'Difficulty Breathing',
'Tiredness and fatigue',
'Chest pain',
'Rapid heartbeat',
'Headaches',
'Loss of appetite',
'Sweating',
'High temperature',
'Coughing up blood',
'Aching muscles and joints',
'Feeling generally unwell',
'Shivering']}
So it outputs the names of the diseases with the maximum number of matches, and a list of all the associated symptoms, which seemed to be what you were doing.
What's unclear is whether this still has a problem you wanted to solve. You could construct patient_symptoms
however you like and the algorithm is non-destructive, so I would think this works for you?