The internet
2021-06-19 14:33
采纳率: 33.3%
浏览 37

我现在有一个类似于这样的字典套列表 怎样把他们合并成一个 相同key下值的value合并并去重

[{'config': [], 'video': ['create', 'list', 'AIreview', 'download', 'approve', 'modify', 'read'], 'channel': ['create', 'modify', 'read', 'list', 'delete', 'approve'], 'webpage': ['list'], 'organization': ['subsidiaries_list', 'channel_list', 'video_list'], 'account': ['list', 'modify_self', 'read_self'], 'catalog': []}

{'config': [], 'video': ['create', 'list', 'modify', 'read', 'delete', 'AIreview', 'download'], 'channel': ['create', 'modify', 'read', 'list', 'delete', 'approve'], 'webpage': ['list'], 'organization': ['subsidiaries_list', 'channel_list', 'video_list'], 'account': ['list', 'modify_self', 'read_self'], 'catalog': []}]


 

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • Roc-xb 2021-06-19 15:18
    已采纳
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    """
    @author: YangPC
    @time:2021/06/18
    @QQ:327844761
    @微信公众号:ewbang
    """
    
    if __name__ == '__main__':
        d = [{'config': [], 'video': ['create', 'list', 'AIreview', 'download', 'approve', 'modify', 'read'],
              'channel': ['create', 'modify', 'read', 'list', 'delete', 'approve'], 'webpage': ['list'],
              'organization': ['subsidiaries_list', 'channel_list', 'video_list'],
              'account': ['list', 'modify_self', 'read_self'], 'catalog': []},
             {'config': [], 'video': ['create', 'list', 'modify', 'read', 'delete', 'AIreview', 'download'],
              'channel': ['create', 'modify', 'read', 'list', 'delete', 'approve'], 'webpage': ['list'],
              'organization': ['subsidiaries_list', 'channel_list', 'video_list'],
              'account': ['list', 'modify_self', 'read_self'], 'catalog': []}]
    
        new_d = {};
        for item in d:
            for key in item.keys():
                if key not in new_d:
                    new_d[key] = item[key]
                else:
                    new_d[key] = list(set(new_d[key] + item[key]))
    
        print(new_d)
    

    若有帮助,望采纳!

    已采纳该答案
    1 打赏 评论

相关推荐 更多相似问题