我们有病人名单,诊断和住院时间。我们还有一本包含诊断和平均住院时间的字典。生成一个输出列表,列出患者,以及患者的停留时间是否“太长”、“太短”、“恰到好处”
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]
]如何将列表中的第三个值与avg_los的相应字典值中的值进行比较?
例如:
Boal接受了1.1天的医疗检查/评估。如果我将它与医疗检查/评估的avg_los进行比较,我得到的值是3.2。3.2大于1.1,所以我想输出“太长”。如果字典值小于列表值,则输出“太小”
如何使用for循环在python中对此进行编码?
发布于 2021-03-01 10:58:26
使用子列表中的第二个值来查找字典中的参考值。然后比较这两个值。对于患者索引patient_num ...
procedure = patients[patient_num][1]
patient_los = patients[patient_num][2]
reference = avg_los[procedure]
if patient_los > reference:
print("Too long")明白了?如果遇到问题,可以使用更多的print语句来跟踪程序正在执行的操作。一旦你有了这个想法,你就可以组合几行代码来缩短代码。
发布于 2021-03-01 10:58:51
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)发布于 2021-03-01 10:59:08
这将是你的问题的解决方案。
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")https://stackoverflow.com/questions/66415983
复制相似问题