简体   繁体   English

如何将 map 列表值放入字典并将列表值与字典值进行比较?

[英]How to map list value into a dictionary and compare list value to the dictionary value?

We have a list of patients, diagnosis, and length of stay.我们有一份患者名单、诊断和住院时间。 We also have a dictionary that contains diagnosis and average length of stay.我们还有一本包含诊断和平均住院时间的字典。 Produce an output list that lists the patient and an indicator if the patient's stay was 'too long', 'too short', 'just right'生成一个 output 列表,其中列出了患者以及患者停留时间是否“太长”、“太短”、“恰到好处”的指示符



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

How can I compare the third value in the list to the value in the corresponding dictionary value of avg_los?如何将列表中的第三个值与 avg_los 的相应字典值中的值进行比较?

For example:例如:

Boal has undergone Medical examination/evaluation with a time frame of 1.1 days. Boal 已在 1.1 天的时间内接受了体检/评估。 If i compare that to avg_los for a medical examination/evaluation the value I get is 3.2.如果我将其与体检/评估的 avg_los 进行比较,我得到的值为 3.2。 3.2 is >1.1 so i want to output "too long". 3.2 > 1.1 所以我想 output “太长”。 If the dictionary value is < than list value then output "too little"如果字典值小于列表值,则 output “太少”

How can I code this in python using a for loop?如何使用 for 循环在 python 中对此进行编码?

You use the second value in the sublist to find the reference value in the dict.您使用子列表中的第二个值来查找字典中的参考值。 Then you compare the two values.然后比较这两个值。 For a patient index patient_num ...对于患者索引patient_num ...

procedure   = patients[patient_num][1]
patient_los = patients[patient_num][2]
reference   = avg_los[procedure]
if patient_los > reference:
    print("Too long")

Got it?知道了? If you have trouble, use more print statements to track what the program is doing.如果遇到问题,请使用更多print语句来跟踪程序正在执行的操作。 Once you have the idea, you can combine lines to shorten your code.一旦你有了这个想法,你就可以组合几行来缩短你的代码。

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)

This would be the solution to your problem.这将是您问题的解决方案。

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

You can do it by iterating through all the patient-diagnoses.您可以通过遍历所有患者诊断来做到这一点。 Use the diagnose as key to obtain the average length of stay from the dictionary.使用诊断作为关键字从字典中获取平均停留时间。 Compare it to the length of stay of the patient.将其与患者的住院时间进行比较。

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

This outputs这输出

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM