这是一个作业或“家庭作业”的问题,就像你们说的那样,我只是在一开始就澄清了。我是Python新手,我真的对这类问题感到困惑。
问题是:护理医院想知道病人人数最多的医疗专业。假设病人的身份与病人所访问的医学专业一起存储在一个列表中。医学专业的详细信息存储在字典中如下:{“P”:“儿科”,“O”:“骨科”,"E":"ENT }
编写一个函数,找出尽可能多的病人访问的医学专业,并返回专科的名称。
我尝试的代码:
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
发布于 2018-12-22 09:27:53
这应该可以做到:
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)输出
Pediatrics首先,你需要按专业来统计每一个病人:
# 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字典中返回'P'的值,
return medical_speciality[most_visited_speciality]在本例中:'Pediatrics'。
进一步
发布于 2018-12-22 09:43:07
如果您受限于“将病人的id与病人所访问的医疗专业一起存储在list__中”,请使用以下优化和统一的方法:
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))产出:
Pediatrics养成“良好实践”的习惯。
发布于 2018-12-22 09:27:31
你快到了。
在for循环中,words保存字符串P或E。现在,您只需要使用它调用字典中的键:
示例:当word为'P‘时,要获得值,您可以使用medical_speciality['P']获取值Pediatrics。所以我们就把它包括在你的功能里。
接下来,max不像您在这里想的那样工作。你需要一种方法来计算“P”或“E”出现的次数,然后你真的只想要那个最大值。
我也会把你的那部分
speciality=max(speciality_list)
return speciality`在for循环之外,当您想要整个列表的最大值时,当它当前在每次迭代之后执行max和return时,这是不需要的。
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)输出:
>>> print(speciality)
>>> Pediatricshttps://stackoverflow.com/questions/53894424
复制相似问题