How to generate output from a list and dictionary both and match the resulting output from them to the dictionary value?

Viewed 1659

This is sort of an assignment or 'homework' problem as you people say and I clarify it in the beginning only. I am new to Python and I really get confused with this kind of problems.

The problem stated is : Care hospital wants to know the medical speciality visited by the maximum number of patients. Assume that the patient id of the patient along with the medical speciality visited by the patient is stored in a list. The details of the medical specialities are stored in a dictionary as follows: { "P":"Pediatrics", "O":"Orthopedics", "E":"ENT }

Write a function to find the medical speciality visited by the maximum number of patients and return the name of the speciality.

Code I attempted:

def max_visited_speciality(patient_medical_speciality_list,medical_speciality):
    speciality_list=[]
    for words in patient_medical_speciality_list:
        if words in medical_speciality:
                speciality_list.append(words)
                speciality=max(speciality_list)
                return speciality

#provide different values in the list and test your program
patient_medical_speciality_list=[301,'P',302, 'P' ,305, 'P' ,401, 'E' ,656, 'E']
medical_speciality={"P":"Pediatrics","O":"Orthopedics","E":"ENT"}
speciality = max_visited_speciality(patient_medical_speciality_list,medical_speciality)
print(speciality)

Sample Input : [101,P,102,O,302,P,305,P]

Expected Output : Pediatrics

Output I am getting : P

4 Answers

This should do it:

def max_visited_speciality(patient_medical_speciality_list, medical_speciality):

    # count each speciality patients
    counts = {}
    for _, speciality in zip(patient_medical_speciality_list[::2], patient_medical_speciality_list[1::2]):
        counts[speciality] = counts.get(speciality, 0) + 1

    # get most visited speciality by count of it's patients
    most_visited_speciality = max(medical_speciality, key=lambda e: counts.get(e, 0))

    # return value of most visited speciality
    return medical_speciality[most_visited_speciality]


# provide different values in the list and test your program
patient_medical_speciality_list = [301, 'P', 302, 'P', 305, 'P', 401, 'E', 656, 'E']
medical_speciality = {"P": "Pediatrics", "O": "Orthopedics", "E": "ENT"}
speciality = max_visited_speciality(patient_medical_speciality_list, medical_speciality)
print(speciality)

Output

Pediatrics

First you need to count each patient by speciality:

# count each speciality patients
    counts = {}
    for _, speciality in zip(patient_medical_speciality_list[::2], patient_medical_speciality_list[1::2]):
        counts[speciality] = counts.get(speciality, 0) + 1

After that counts = {'E': 2, 'P': 3}, because there are 3 patients that visited 'P' and 2 that visited 'E'. Then use those values as keys in max:

most_visited_speciality = max(medical_speciality, key=lambda e: counts.get(e, 0))

This returns 'P' the most visited speciality, then return the value of 'P' in the medical_speciality dictionary,

return medical_speciality[most_visited_speciality]

in this case: 'Pediatrics'.

Further

  1. The documentation to max.
  2. The documentation on the get method of dict.

If you're restricted with requirement "keep patient id of the patient along with the medical speciality visited by the patient is stored in a list", use the following optimized and unified approach:

from collections import Counter


class MedicalSpecialityError(Exception):
    pass


medical_speciality_map = {"P": "Pediatrics", "O": "Orthopedics", "E": "ENT"}
patient_medical_speciality_list = [301, 'P', 302, 'P', 305, 'P', 401, 'E', 656, 'E']


def max_visited_speciality(patient_medical_speciality_list: list):
    counts = Counter(s for s in patient_medical_speciality_list if str(s).isalpha())
    try:
        med_spec = medical_speciality_map[counts.most_common()[0][0]]
    except IndexError:
        raise MedicalSpecialityError('Bad "patient_medical_speciality_list"')
    except KeyError:
        raise MedicalSpecialityError('Unknown medical speciality key')

    return med_spec


print(max_visited_speciality(patient_medical_speciality_list))

The output:

Pediatrics

P.S. Make a habit of "good practices".

You're almost there.

in your for loop, words is holding the string P, or E. Now you just need to use that to call the key in your dictionary:

example: when word is 'P', to get the value, you'd use medical_speciality['P'] to get the value Pediatrics. So we'll just include that in your function.

Next, max doesn't work as you're thinking here. You'll need a way to count up the number of times a 'P' or 'E' show up, then you really just want that max value.

I would also move your part of

speciality=max(speciality_list)
return speciality`

outside the for loop, as you want the max of that full list, where as it's currently doing max and return after each iteration, which isn't needed.

def max_visited_speciality(patient_medical_speciality_list,medical_speciality):
    speciality_list=[]
    for words in patient_medical_speciality_list:
        if words in medical_speciality:
                speciality_list.append(words)

    counts = dict(map(lambda x  : (x , speciality_list.count(x)) , speciality_list))
    most_visited_speciality = max(counts, key=lambda e: counts.get(e, 0))
    return medical_speciality[most_visited_speciality]

#provide different values in the list and test your program
patient_medical_speciality_list=[301,'P',302, 'P' ,305, 'P' ,401, 'E' ,656, 'E']
medical_speciality={"P":"Pediatrics","O":"Orthopedics","E":"ENT"}
speciality = max_visited_speciality(patient_medical_speciality_list,medical_speciality)
print(speciality)

Output:

>>> print(speciality)
>>> Pediatrics
def max_visited_speciality(patient_medical_speciality_list,medical_speciality):
    # write your logic here
    a=len(patient_medical_speciality_list)
    b=patient_medical_speciality_list[1:a:2]
    c=[]
    d=[]
    for key in medical_speciality:
        c.append(key)
    #print(c)
    for i in c:
        d.append(b.count(i))
    #print(d)
    x=d.index(max(d))
    #print(x)
    return medical_speciality[c[x]]



#return speciality


patient_medical_speciality_list=[301,'P',302, 'P' ,305, 'P' ,401, 'E' ,656, 'E']
medical_speciality={"P":"Pediatrics","O":"Orthopedics","E":"ENT"}
speciality=max_visited_speciality(patient_medical_speciality_list,medical_speciality)
print(speciality)
Related