如何从列表和字典中生成输出,并将它们的结果输出与字典值相匹配?

2024-05-15 15:02:28 发布

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

正如你们所说的,这是一个作业或“家庭作业”问题,我只是在一开始就澄清了。我是Python新手,我真的对这种问题感到困惑。在

问题是: 护理医院想知道最大数量的病人访问的医疗专业。假设患者的患者id以及患者访问的医疗专业都存储在一个列表中。医学专业的详细信息存储在字典中,如下所示: { “P”:“儿科”, “O”:“骨科”, “E”:“耳鼻喉科” }在

编写一个函数来查找最大患者数访问的医学专业,并返回该专业的名称。在

我尝试的代码:

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)

样本输入:[101,p,102,O,302,p,305,p]

预期产出:儿科

我得到的输出:p


Tags: in患者专业作业maxlistmedicalwords
3条回答

这应该做到:

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)

输出

^{pr2}$

首先,您需要按专业对每个患者进行计数:

# 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

在那之后,counts = {'E': 2, 'P': 3},因为有3名患者访问了“p”,2名患者访问了“E”。然后将这些值用作max中的关键点:

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

这将返回'P'访问次数最多的专业,然后返回medical_speciality字典中{}的值

return medical_speciality[most_visited_speciality]

在本例中:'Pediatrics'。在

进一步

  1. 文档到max。在
  2. dict的get方法的文献

你快到了。在

for循环中,words持有字符串P,或{}。现在您只需使用它来调用字典中的键:

示例:当word是'p'时,为了得到值,您应该使用medical_speciality['P']来获得值Pediatrics。所以我们就把它包含在你的函数中。在

接下来,max并不像您在这里所想的那样工作。你需要一种方法来计算“P”或“E”出现的次数,然后你真的只需要这个最大值。在

我也会把你的部分

speciality=max(speciality_list)
return speciality`

for循环之外,如果您想要该完整列表的最大值,在每次迭代之后它当前正在执行max和{},这是不需要的。在

^{pr2}$

输出:

>>> print(speciality)
>>> Pediatrics

如果您受到要求的限制,请使用以下优化和统一的方法来保存患者的患者id以及患者访问的医疗专业知识存储在列表中“”:

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

输出:

^{pr2}$

养成良好的习惯。在

相关问题 更多 >