# 2个列表中的字典,根据字典的指定字段:tag_info去重 a = [{'version': '13', 'tag_info': 'tag:shifou-3232661', 'commit_id': '720eb278b7efe5b259413190a75c6f2be984bade'}, {'version': '12', 'tag_info': 'tag:shifou-3232662', 'commit_id': '720eb278b7efe5b259413190a75c6f2be984bade'}, {'version': '11', 'tag_info': 'tag:shifou-3232663', 'commit_id': '720eb278b7efe5b259413190a75c6f2be984bade'}, {'version': '5', 'tag_info': 'tag:shifou-3232664', 'commit_id': '720eb278b7efe5b259413190a75c6f2be984bade'}] b = [{'version': '133', 'tag_info': 'tag:shifou-3232666', 'commit_id': '720eb278b7efe5b259413190a75c6f2be984bade'}, {'version': '122', 'tag_info': 'tag:shifou-3232666', 'commit_id': '720eb278b7efe5b259413190a75c6f2be984bade'}, {'version': '113', 'tag_info': 'tag:shifou-3232663', 'commit_id': '720eb278b7efe5b259413190a75c6f2be984bade'}, {'version': '54', 'tag_info': 'tag:shifou-3232664', 'commit_id': '720eb278b7efe5b259413190a75c6f2be984bade'}] # 输出 # c = [ # {'tag_info': 'tag:shifou-323266', 'commit_id': '720eb278b7efe5b259413190a75c6f2be984bade'}, # {'tag_info': 'tag:shifou-323266', 'commit_id': '720eb278b7efe5b259413190a75c6f2be984bade'} # ] tag_info_b = [ele["tag_info"] for ele in b] result = [] # 求a、b的交集: for ele in a: tag_info = ele["tag_info"] if tag_info in tag_info_b: result.append(ele) print(result)
# 一个列表中包含多个字典,根据字典key去重 # 待合并的项目信息,不同需求中可能包含重复的项目(不同需求包含相同project_id), 但分支不同,需要把其中每个分支都merge到master pc_values = ProjectConfig.objects.filter(demand_id__in=demand_id_lst).values("demand_id", "branch", "project_id", "project_name", "git_url", "env", "stage") # 合并分支前按project_id、branch去重, 正常来说:不同需求的项目,分支一定不同 result = [] _tmp = [] for i in pc_values: unique_key = str(i["project_id"]) + i["branch"] if unique_key not in _tmp: result.append(i) _tmp.append(unique_key)