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)
  • 相关阅读:
    android 中使用AsyncTask实现简单的异步编程
    android TextView 垂直自动滚动
    (转)c3p0配置大全
    Android中在底端显示选项卡
    android 中ImageView的scaletype属性
    android 权限大全
    Palm应用开发之六 常用命令及debug
    android spinner 实现Text 和 value
    [转载]【职场宝典】面试官如何看待学历?
    起跑
  • 原文地址:https://www.cnblogs.com/yum777/p/14803770.html
Copyright © 2011-2022 走看看