如何将列表值映射到字典并将列表值与字典值进行比较?

时间:2021-03-01 02:45:15

标签: python loops

我们有一份患者名单、诊断和住院时间。我们还有一本字典,其中包含诊断和平均住院时间。生成一个输出列表,列出患者并指示患者的停留时间是否“太长”、“太短”、“恰到好处”



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 中编码?

4 个答案:

答案 0 :(得分:0)

您使用子列表中的第二个值在字典中查找引用值。然后比较这两个值。对于患者索引 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 语句来跟踪程序正在执行的操作。一旦有了想法,您就可以合并行以缩短代码。

答案 1 :(得分:0)

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)

答案 2 :(得分:0)

这将是您问题的解决方案。

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

答案 3 :(得分:0)

您可以通过遍历所有患者诊断来实现。使用诊断作为关键字从字典中获取平均住院时间。将其与患者的住院时间进行比较。

for name, diagnose, los in patients:
    if (avg_los[diagnose] < los):
        print(f'{name,diagnose}: too long')
    elif (avg_los[diagnose] > los):
        print(f'{name,diagnose}: too short')
    else:
        print(f'{name,diagnose}: just right')

这个输出

('Boal', 'Medical examination/evaluation'): too short
('Boal', 'Other complications of pregnancy'): too long
('Jones', 'Liveborn'): just right
('Ashbury', 'Forceps delivery'): too short