正如您所说的,这是一种作业或“家庭作业”问题,我仅在开始时予以澄清。我是Python的新手,我真的对这种问题感到困惑。
指出的问题是: 护理医院希望了解最多患者数所拜访的医学专业。假设将患者的患者ID以及患者所拜访的医学专业知识存储在列表中。医学专业的详细信息存储在字典中,如下所示: { “ P”:“儿科”, “ O”:“骨科”, “ E”:“ ENT }
编写一个函数以查找最多患者数所访问的医学专业,并返回该专业的名称。
我尝试的代码:
// I think UNIX open() will also work.
FILE *file = fopen("your_image.png", "r");
样本输入:[101,P,102,O,302,P,305,P]
预期产量:儿科
我得到的输出是:P
答案 0 :(得分:4)
这应该做到:
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'
返回最常访问的专长,然后在'P'
字典中返回medical_speciality
的值,
return medical_speciality[most_visited_speciality]
在这种情况下:'Pediatrics'
。
进一步
答案 1 :(得分:2)
如果您受到要求的限制,“将患者的病历和患者所拜访的医学专业知识存储在列表中” ,请使用以下命令优化和统一的方法:
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
P.S。养成“良好做法”的习惯。
答案 2 :(得分:1)
你快到了。
在您的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)
>>> Pediatrics
答案 3 :(得分:0)
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)