如何在python中合并两个带字典的字典?

时间:2017-04-18 06:50:18

标签: python dictionary merge

以下是来自大量类似输入文件的示例输入。

{
    "V-Su7890": [
        [
            {
                "url": "www.talent.com",
                "tid": "V-Su7890",
                "id": "58ff787ffbad487b2c",
                "company_name": "Talent Ltd"
            }
        ],
        [
            {
                "socials": ["facebook", "linkedin", "twitter"],
                "title": "title of the pag",
                "contact": ["+9563802140"],
                "email": "email_id1"
            },
            {
                "socials": ["facebook", "twitter", "linkedin"],
                "title": "next title of the page",
                "contact": ["+919765983442"],
                "email": "email_id2"
            }
        ]
    ]
}

我必须将当前字典的第二个列表的所有子字段合并到一个没有重复值的字典中,然后将字典作为值存储到密钥" V-Su7890"。

所需的输出是:

{
    "V-Su7890": [
        [
            {
                "url": "www.talent.com",
                "tid": "V-Su7890",
                "id": "58ff787ffbad487b2c",
                "company_name": "Talent Ltd"
            }
        ],
        [
            {
                "socials": ["facebook", "linkedin", "twitter"],
                "title": ["title of the pag", "next title of the page"],
                "contact": ["+9563802140", "+919765983442"],
                "email": ["email_id","email_id2"]
            }
        ]
    ]
}

请帮助我理解并解决这个问题。

2 个答案:

答案 0 :(得分:1)

您可以使用setdefault()插入值为default的键(此处可以使用空列表),如果新项目不存在,则使用extend列表。

for k,v in a.items():
    tmp={}
    for i in v[1]:
        for k1,v2 in i.items():
            if isinstance(v2,list):
                tmp.setdefault(k1,[]).extend(i for i in v2 if i not in tmp[k1])
            else:
                tmp.setdefault(k1,[]).append(v2)
    a[k]=[v[0],[tmp]]
print(a)

结果:

{
  'V-Su7890': [
    ...
    [
      {
        'contact': ['+9563802140','+919765983442'],
        'socials': ['facebook','linkedin','twitter'],
        'email': ['email_id1','email_id2'],
        'title': ['title of the pag','next title of the page']
      }
    ]
  ]
}

答案 1 :(得分:0)

我们假设您将完整的dict存储在变量V中。我们将socialstitle等的值存储在一个集合中以避免重复值。稍后,我们将把这些集转换为列表。这是解决方案:

V = k["V-Su7890"][1]
new_dict = {}

for v in V:
    for key, value in v.iteritems():
        if not new_dict.get(key, None):
            new_dict[key] = set()

        if isinstance(value, list):
            for val in value:
                new_dict[key].add(val)
        else:
            new_dict[key].add(value)

# Converting the sets to list
for key, value in new_dict.iteritems():
    new_dict[key] = list(value)

k["V-Su7890"][1] = [new_dict]