【发布时间】:2016-03-01 17:10:30
【问题描述】:
这是一个棘手的小问题,我被困住了。我有一个字典列表,如下所示:
[{'medication_name': 'Actemra IV',
'total_prescriptions': 4},
{'medication_name': 'Actemra IV',
'total_prescriptions': 3},
{'medication_name': 'Actemra IV',
'total_prescriptions': 1},
{'medication_name': 'Actemra IV',
'total_prescriptions': 6},
{'medication_name': 'Actemra SC',
'total_prescriptions': 8},
{'medication_name': 'Actemra SC',
'total_prescriptions': 1},
{'medication_name': 'Actemra SC',
'total_prescriptions': 3}]
我想要做的是总结每种药物的各种字典的总处方,并将最终总和作为每个字典的条目附加如下:
[{'medication_name': 'Actemra IV',
'total_prescriptions': 4,
'final_count': 14},
{'medication_name': 'Actemra IV',
'total_prescriptions': 3,
'final_count': 14},
{'medication_name': 'Actemra IV',
'total_prescriptions': 1,
'final_count': 14},
{'medication_name': 'Actemra IV',
'total_prescriptions': 6,
'final_count': 14},
{'medication_name': 'Actemra SC',
'total_prescriptions': 8,
'final_count': 12},
{'medication_name': 'Actemra SC',
'total_prescriptions': 1,
'final_count': 12},
{'medication_name': 'Actemra SC',
'total_prescriptions': 3,
'final_count': 12}
]
最有效的方法是什么?
【问题讨论】:
-
你为什么选择了另一个答案,和我的一样,只是慢了点?
-
哦,我看到他对其进行了编辑以纠正其不必要的复杂性。
标签: list python-2.7 dictionary