【python】 list dict 字典合并

# -- coding:utf-8 -- # 初始列表 job_groups = [ {"project_id": 25, "description": "mdwsw 666", "count_job_group": 8}, {"project_id": 12, "description": "mdwsw 222", "count_job_group": 10}, {"project_id": 19, "description": "mdwsw 111", "count_job_group": 118}, {"project_id": 14, "description": "mdwsw 444", "count_job_group": 2}, {"project_id": 15, "description": "mdwsw 555", "count_job_group": 2} ] jobs = [ {"project_id": 12, "description": "mdwsw 222", "count_job": 2}, {"project_id": 19, "description": "mdwsw 111", "count_job": 5}, {"project_id": 22, "description": "mdwsw 333", "count_job": 1} ] # 合并列表 def merge_lists(list1, list2): merged = list1.copy() # 创建list1的副本以避免修改原始列表 for item2 in list2: # 查找是否有相同的project_id和description found = False for item1 in merged: if item1['project_id'] == item2['project_id'] and item1['description'] == item2['description']: # 如果找到,合并字典 item1.update(item2) found = True break if not found: # 如果没有找到,添加到合并列表中 merged.append(item2) return merged # 执行合并 merged_list = merge_lists(job_groups, jobs) # 打印合并后的列表 print(merged_list)
上一篇:怎么屏蔽统计系统统计到的虚假ip


下一篇:【CSS Tricks】深入聊聊前端编写css的方法论-ACSS 规范