zoukankan      html  css  js  c++  java
  • 合并去重

    # 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)
  • 相关阅读:
    聚集索引
    第一天 尝试Thread
    sql 分区函数
    sql 查询表定义
    千万数量级分页存储过程
    成语解释
    sql 分组查询满足条件所以数据
    sql存储过程
    联表更新的反思
    从表保存了主表的id,以分号分隔,怎么样用一条sql搞定主表满足条件的查询? 不希望单独写存储过程,或者后台拆成int后传进来,就一条sql 搞定,一条
  • 原文地址:https://www.cnblogs.com/yum777/p/14803770.html
Copyright © 2011-2022 走看看