gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:59:36 24 4
gpt4 key购买 nike

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

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

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'}]}
]

最佳答案

这是一个展示 itertools.groupby 力量的完美场景。请注意,我假设 haps、state 和 ads 将出现在所有词典中,并且重复次数相似

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)

输出将如下所示

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

关于python - python 将具有相同键值对的多个字典合并为一个字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55916939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com