简体   繁体   中英

How to map list value into a dictionary and compare list value to the dictionary value?

We have a list of patients, diagnosis, and length of stay. We also have a dictionary that contains diagnosis and average length of stay. Produce an output list that lists the patient and an indicator if the patient's stay was 'too long', 'too short', 'just right'



avg_los = {
    "Hemolytic jaundice and perinatal jaundice" : 2,
    "Medical examination/evaluation" : 3.2,
    "Liveborn" : 3.2,
    "Trauma to perineum and vulva" : 2.1,
    "Normal pregnancy and/or delivery" : 2,
    "Umbilical cord complication" : 2.1,
    "Forceps delivery" : 2.2,
    "Administrative/social admission" : 4.2,
    "Prolonged pregnancy" : 2.4,
    "Other complications of pregnancy" : 2.5
}

#List
patients = [
    ['Boal', 'Medical examination/evaluation', 1.1],
    ['Boal', 'Other complications of pregnancy', 3.3],
    ['Jones', 'Liveborn', 3.2],
    ['Ashbury', 'Forceps delivery', 2.0]
]

How can I compare the third value in the list to the value in the corresponding dictionary value of avg_los?

For example:

Boal has undergone Medical examination/evaluation with a time frame of 1.1 days. If i compare that to avg_los for a medical examination/evaluation the value I get is 3.2. 3.2 is >1.1 so i want to output "too long". If the dictionary value is < than list value then output "too little"

How can I code this in python using a for loop?

You use the second value in the sublist to find the reference value in the dict. Then you compare the two values. For a patient index patient_num ...

procedure   = patients[patient_num][1]
patient_los = patients[patient_num][2]
reference   = avg_los[procedure]
if patient_los > reference:
    print("Too long")

Got it? If you have trouble, use more print statements to track what the program is doing. Once you have the idea, you can combine lines to shorten your code.

avg_los = {
    "Hemolytic jaundice and perinatal jaundice" : 2,
    "Medical examination/evaluation" : 3.2,
    "Liveborn" : 3.2,
    "Trauma to perineum and vulva" : 2.1,
    "Normal pregnancy and/or delivery" : 2,
    "Umbilical cord complication" : 2.1,
    "Forceps delivery" : 2.2,
    "Administrative/social admission" : 4.2,
    "Prolonged pregnancy" : 2.4,
    "Other complications of pregnancy" : 2.5
}

#List
patients = [
    ['Boal', 'Medical examination/evaluation', 1.1],
    ['Boal', 'Other complications of pregnancy', 3.3],
    ['Jones', 'Liveborn', 3.2],
    ['Ashbury', 'Forceps delivery', 2.0]
]

for patient in patients:
    diagnosis = patient[1]
    time_frame = avg_los.get(diagnosis)

    if patient[2] > time_frame :
        message = 'too long'
    elif patient[2] < time_frame :
        message = 'too short'
    elif patient[2] == time_frame :
        message = 'exact'
    print(message)

This would be the solution to your problem.

for patient in patients:
    diagnosis = patient[1]
    length_of_stay = patient[2]

    average_length_of_stay = avg_los.get(diagnosis)
    if average_length_of_stay is not None:
        if average_length_of_stay < length_of_stay:
            print("too long")
        elif average_length_of_stay == length_of_stay:
            print("just right")
        else:
            print("too short")
    else:
        print("Diagnosis is not found")

You can do it by iterating through all the patient-diagnoses. Use the diagnose as key to obtain the average length of stay from the dictionary. Compare it to the length of stay of the patient.

for name, diagnose, los in patients:
    if (avg_los[diagnose] < los):
        print(f'{name,diagnose}: too long')
    elif (avg_los[diagnose] > los):
        print(f'{name,diagnose}: too short')
    else:
        print(f'{name,diagnose}: just right')

This outputs

('Boal', 'Medical examination/evaluation'): too short
('Boal', 'Other complications of pregnancy'): too long
('Jones', 'Liveborn'): just right
('Ashbury', 'Forceps delivery'): too short

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