如何合并字典列表

时间:2020-07-02 16:51:48

标签: python

我有一个字典列表,

[{'task_count': 2, 'task_status': 'approved', 'application_id': 252},
 {'task_count': 4, 'task_status': 'assigned', 'application_id': 252},
 {'task_count': 4, 'task_status': 'assigned_for_review', 'application_id': 252},
 {'task_count': 2, 'task_status': 'ready_for_review', 'application_id': 252},
 {'task_count': 3, 'task_status': 'assigned', 'application_id': 234},
 {'task_count': 11, 'task_status': 'assigned', 'application_id': 232},
 {'task_count': 2, 'task_status': 'cancelled', 'application_id': 232}]

我想根据应用程序ID组合它们

1 个答案:

答案 0 :(得分:0)

也许这就是您想要的

list_dict = [{'task_count': 2, 'task_status': 'approved', 'application_id': 252}, {'task_count': 4, 'task_status': 'assigned', 'application_id': 252}, {'task_count': 4, 'task_status': 'assigned_for_review', 'application_id': 252}, {'task_count': 2, 'task_status': 'ready_for_review', 'application_id': 252}, {'task_count': 3, 'task_status': 'assigned', 'application_id': 234}, {'task_count': 11, 'task_status': 'assigned', 'application_id': 232}, {'task_count': 2, 'task_status': 'cancelled', 'application_id': 232}]

result = {x["application_id"]:x for x in list_dict}.values()

print(result)

输出

dict_values([{'task_count': 2, 'task_status': 'ready_for_review', 'application_id': 252}, {'task_count': 3, 'task_status': 'assigned', 'application_id': 234}, {'task_count': 2, 'task_status': 'cancelled', 'application_id': 232}])