将具有相同键值对的多个字典合并到一个字典python

时间:2019-04-30 08:37:01

标签: python python-3.x dictionary for-loop

我有一个字典列表存储在列表中。我想将相同的字典合并为一个。我有三个领域。 Task_id提供要检查的字段。 value是该字段的值。首先,它检查dict中的值,并为要合并的dict创建一个新的dict。如果所有值都相同,并且其中一个值只是不同,则它将dict合并为一个。如何使其成为可能

这是我尝试过的示例代码:

field_to_be_check ="state"
merger = ["city", "ads"]
merge_name = ["cities", "my_ads"]

data = [
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad1', 'city': 'tirunelveli'}, 
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad4', 'city': 'nagerkoil'}, 
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad1', 'city': 'tuticorin'}, 
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad1', 'city': 'madurai'}, 
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad1', 'city': 'chennai'}, 
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'palakad'}, 
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'guruvayor'}, 
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'kolikodu'}, 
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'kottayam'}, 
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'idukki'}, 
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Akola'}, 
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Washim'}, 
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Jalna'}, 
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Nanded'}, 
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Latur'}
]

d = []
list1 = []
for item in data:
    value = item[field_to_be_check]
    inserted = False
    for l in list1: 
        if l[field_to_be_check] == value:
            inserted = True
            for m_name in merge_name:

    if inserted == False:
        list1.append(item)

print(list1)

必需的输出:

   [
    {'state': 'tamil nadu','my_ads':[{'ads': 'ad1'},{'ads': 'ad4'}], 'cities':[{'city': 'tirunelveli'},{'city': 'nagerkoil'},{'city': 'tuticorin'},{'city': 'madurai'},{'city': 'chennai'}]}, 
    {'state': 'kerala',,'my_ads':[{'ads': 'ad2'}], 'cities': [{'city': 'palakad'},{'city': 'guruvayor'},{'city': 'kolikodu'},{'city': 'kottayam'},{'city': 'idukki'}]}, 
    {'state': 'mumbai', 'my_ads':[{'ads': 'ad3'}],'cities':[{'city': 'Akola'},{'city': 'Washim'},{'city': 'Jalna'},{'city': 'Nanded'},{'city': 'Latur'}]}
    ]

2 个答案:

答案 0 :(得分:1)

field_to_be_check ="state"
merger = ["city", "ads"]
merge_name = ["cities", "my_ads"]

data = [
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad1', 'city': 'tirunelveli'}, 
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad4', 'city': 'nagerkoil'}, 
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad1', 'city': 'tuticorin'}, 
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad1', 'city': 'madurai'}, 
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad1', 'city': 'chennai'}, 
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'palakad'}, 
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'guruvayor'}, 
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'kolikodu'}, 
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'kottayam'}, 
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'idukki'}, 
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Akola'}, 
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Washim'}, 
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Jalna'}, 
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Nanded'}, 
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Latur'}
]
# merger and merge_name must be one to one.
the_dict = {m:mn for m, mn in zip(merger, merge_name)}
# {"city":"cities", "ads":"my_ads"}  merge_name
newdata = data.copy()
# create new_ret as result
new_ret = [{field_to_be_check:i, **{i:[] for i in merge_name}} for i in set([i[field_to_be_check] for i in data])]
# print(new_ret, "this is new_ret")
for val in new_ret:
    for k in newdata:
        if val[field_to_be_check] != k[field_to_be_check]:
            continue
        tmp = {i:k[i] for i in merger}
        for single in tmp:
            if {single:tmp[single]} not in val[the_dict[single]]:
                val[the_dict[single]].append({single:tmp[single]})
print(new_ret)

答案 1 :(得分:1)

这是了解itertools.groupby强大功能的完美方案 请注意,我假设所有字典中都会出现haps,state和ad,并且在重复中也是如此

$code->discount->url

输出看起来像

from itertools import groupby

field_to_be_check =  "state"
merger = ["city", "ads"]
merge_name = ["cities", "my_ads"]

data = [
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad1', 'city': 'tirunelveli'},
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad4', 'city': 'nagerkoil'},
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad1', 'city': 'tuticorin'},
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad1', 'city': 'madurai'},
{'haps': 'hap0', 'state': 'tamil nadu', 'ads': 'ad1', 'city': 'chennai'},
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'palakad'},
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'guruvayor'},
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'kolikodu'},
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'kottayam'},
{'haps': 'hap1', 'state': 'kerala', 'ads': 'ad2', 'city': 'idukki'},
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Akola'},
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Washim'},
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Jalna'},
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Nanded'},
{'haps': 'hap2', 'state': 'mumbai', 'ads': 'ad3', 'city': 'Latur'}
]

#Function to make the merger lists
def process_group(group, merger_item):

    item_set = set()
    item_list = []
    for item in group:
        item_set.add(item[merger_item])

    for item in item_set:
        item_list.append({merger_item: item})

    return item_list

#Group on haps, state and ads
grp = groupby(data,key=lambda x:(x[field_to_be_check]))
result = []

#Iterate through the group and build your result list
for model, group in grp:
    cities_dict = {}

    cities_dict[field_to_be_check] = model

    group_list = list(group)

    #Make the list for merger fields
    for idx, name in enumerate(merger):
        cities_dict[merge_name[idx]] = process_group(group_list, name)

    result.append(cities_dict)

print(result)
相关问题