用Python从不同字典的列表中创建字典

huangapple go评论66阅读模式
英文:

Python make dictionary from list of different dictionary

问题

我有以下字典列表:

dict_list = [{'type': 'hr', 'data': '{"Name": "Name1","City": "city1", "EMail": "email1@gmail.com"}'},
             {'type': 'hr', 'data': '{"Name": "Name2","City": "city1", "EMail": "email2@gmail.com"}'},
             {'type': 'it', 'data': '{"Name": "Name3","City": "city3", "EMail": "email3@gmail.com"}'},
             {'type': 'it', 'data': '{"Name": "Name4","City": "city2", "EMail": "email4@gmail.com"}'},
             {'type': 'op', 'data': '{"Name": "Name5","City": "city1", "EMail": "email5@gmail.com"}'}]

我想要从现有的字典列表中创建一个新的字典列表,如果在dict_list中有相同的"type",则将所有数据附加在一起,以创建如下所示的预期字典列表:

expected_dict_list = [{'type':'hr', 'data':'[{"Name":"Name1","City":"city1","EMail":"email1@gmail.com"}, {"Name":"Name2","City":"city1","EMail":"email2@gmail.com"}]'},                        
                      {'type':'it', 'data':'[{"Name":"Name3","City":"city3","EMail":"email3@gmail.com"},{"Name":"Name4","City":"city2","EMail":"email4@gmail.com"}]'},                        
                      {'type':'op', 'data':'[{"Name":"Name5","City":"city1","EMail":"email5@gmail.com"}]'}]

请帮助实现这个预期输出,以便稍后基于"type"键将数据发布到相应类型的REST API端点。

英文:

I have the following list of dictionaries:

dict_list = [{'type': 'hr', 'data': '{"Name": "Name1","City": "city1", "EMail": "email1@gmail.com"}', 
             {'type': 'hr', 'data': '{"Name": "Name2","City": "city1", "EMail": "email2@gmail.com"}',
             {'type': 'it', 'data': '{"Name": "Name3","City": "city3", "EMail": "email3@gmail.com"}', 
             {'type': 'it', 'data': '{"Name": "Name4","City": "city2", "EMail": "email4@gmail.com"}', 
             {'type': 'op', 'data': '{"Name": "Name5","City": "city1", "EMail": "email5@gmail.com"}']

I would like to create new list of dictionaries from the existing list of dictionaries where If I have the same "type" in dict_list then append all data to make a single list of data dictionary as below expected list of dictionary:

expacted_dict_list = [{'type':'hr', 'data':'[{"Name":"Name1","City":"city1","EMail":"email1@gmail.com"}, {"Name":"Name2","City":"city1","EMail":"email2@gmail.com"}]'},                        
                      {'type':'it', 'data':'[{"Name":"Name3","City":"city3","EMail":"email3@gmail.com"},{"Name":"Name4","City":"city2","EMail":"email4@gmail.com"}]'},                        
                      {'type':'op', 'data':'[{"Name":"Name5","City":"city1","EMail":"email5@gmail.com"}]'}]

Please help to achieve any this expected output.so that later based on key type will post the data to a rest api endpoints for that type.

答案1

得分: 1

以下是您要的代码部分的翻译:

from collections import defaultdict
from pprint import pprint

dict_list = [{'type': 'hr', 'data': {"Name": "Name1","City": "city1", "EMail": "email1@gmail.com"}},
             {'type': 'hr', 'data': {"Name": "Name2","City": "city1", "EMail": "email2@gmail.com"}},
             {'type': 'it', 'data': {"Name": "Name3","City": "city3", "EMail": "email3@gmail.com"}},
             {'type': 'it', 'data': {"Name": "Name4","City": "city2", "EMail": "email4@gmail.com"}},
             {'type': 'op', 'data': {"Name": "Name5","City": "city1", "EMail": "email5@gmail.com"}}]

tempdict = defaultdict(list)
for d in dict_list:
    tempdict[d['type']].append(d['data'])

out_list = [{'type': k, 'data': v} for k,v in tempdict.items()]
pprint(out_list)

输出:

[{'data': [{'City': 'city1', 'EMail': 'email1@gmail.com', 'Name': 'Name1'},
           {'City': 'city1', 'EMail': 'email2@gmail.com', 'Name': 'Name2'}],
  'type': 'hr'},
 {'data': [{'City': 'city3', 'EMail': 'email3@gmail.com', 'Name': 'Name3'},
           {'City': 'city2', 'EMail': 'email4@gmail.com', 'Name': 'Name4'}],
  'type': 'it'},
 {'data': [{'City': 'city1', 'EMail': 'email5@gmail.com', 'Name': 'Name5'}],
  'type': 'op'}]

如果您确认'data'参数周围的引号是错误的,并且它实际上应该是JSON字符串,那么您可以使用以下代码:

import json
tempdict = defaultdict(list)
for d in dict_list:
    tempdict[d['type']].append(json.loads(d['data']))

out_list = [{'type': k, 'data': json.dumps(v)} for k,v in tempdict.items()]

但请确保再次验证数据格式。将JSON嵌套在JSON中是不常见的,但也不是不可能的情况。

英文:

This does what you ask in a few lines.

from collections import defaultdict
from pprint import pprint

dict_list = [{'type': 'hr', 'data': {"Name": "Name1","City": "city1", "EMail": "email1@gmail.com"}}, 
             {'type': 'hr', 'data': {"Name": "Name2","City": "city1", "EMail": "email2@gmail.com"}},
             {'type': 'it', 'data': {"Name": "Name3","City": "city3", "EMail": "email3@gmail.com"}}, 
             {'type': 'it', 'data': {"Name": "Name4","City": "city2", "EMail": "email4@gmail.com"}}, 
             {'type': 'op', 'data': {"Name": "Name5","City": "city1", "EMail": "email5@gmail.com"}}]

tempdict = defaultdict(list)
for d in dict_list:
    tempdict[d['type']].append( d['data'] )

out_list = [{'type': k, 'data': v} for k,v in tempdict.items()]
pprint(out_list)

Output:

[{'data': [{'City': 'city1', 'EMail': 'email1@gmail.com', 'Name': 'Name1'},
           {'City': 'city1', 'EMail': 'email2@gmail.com', 'Name': 'Name2'}],
  'type': 'hr'},
 {'data': [{'City': 'city3', 'EMail': 'email3@gmail.com', 'Name': 'Name3'},
           {'City': 'city2', 'EMail': 'email4@gmail.com', 'Name': 'Name4'}],
  'type': 'it'},
 {'data': [{'City': 'city1', 'EMail': 'email5@gmail.com', 'Name': 'Name5'}],
  'type': 'op'}]

This ASSUMES that the quotes around your 'data' parameter were a mistake. (You also omitted a closing ] on each line -- you should always run the code before you paste it into a question). IF the 'data' member is actually supposed to be a JSON string, then you'd need an extra json.loads step in the first loop, and an extra json.dumps in the second loop.

Followup

If my assumptions were wrong, then you need:

tempdict = defaultdict(list)
for d in dict_list:
    tempdict[d['type']].append( json.loads(d['data']) )

out_list = [{'type': k, 'data': json.dumps(v)} for k,v in tempdict.items()]

But certainly do double check this. It's unusual (but not unheard of) to have JSON nested inside JSON.

答案2

得分: 0

我认为你的格式有一些错误(在第一个缺少 '},第二个多余 ')。
一旦修复,这应该可以运行:

new_dict = dict()

for d in dict_list:
    new_dict[d['type']] = new_dict.get(d['type'], []) + [d['data']]

expacted_dict_list = [{'type': k, 'data': v} for k, v in new_dict.items()]

我将其用作输入:

dict_list = [{'type': 'hr', 'data': '{"Name": "Name1","City": "city1", "EMail": "email1@gmail.com"}'}, 
             {'type': 'hr', 'data': '{"Name": "Name2","City": "city1", "EMail": "email2@gmail.com"}'},
             {'type': 'it', 'data': '{"Name": "Name3","City": "city3", "EMail": "email3@gmail.com"}'}, 
             {'type': 'it', 'data': '{"Name": "Name4","City": "city2", "EMail": "email4@gmail.com"}'}, 
             {'type': 'op', 'data': '{"Name": "Name5","City": "city1", "EMail": "email5@gmail.com"}'}]

输出:

[{'type': 'hr',
  'data': ['{"Name": "Name1","City": "city1", "EMail": "email1@gmail.com"}',
   '{"Name": "Name2","City": "city1", "EMail": "email2@gmail.com"}']},
 {'type': 'it',
  'data': ['{"Name": "Name3","City": "city3", "EMail": "email3@gmail.com"}',
   '{"Name": "Name4","City": "city2", "EMail": "email4@gmail.com"}']},
 {'type': 'op',
  'data': ['{"Name": "Name5","City": "city1", "EMail": "email5@gmail.com"}']}]
英文:

I think you have some errors in your formatting (missing ' and } in the 1st, extra ' in the second.
Once fixed, this should work:

new_dict = dict()

for d in dict_list:
    new_dict[d['type']] = new_dict.get(d['type'], []) + [d['data']]

expacted_dict_list = [{'type': k, 'data': v} for k, v in new_dict.items()]

I'm using this as input:

dict_list = [{'type': 'hr', 'data': '{"Name": "Name1","City": "city1", "EMail": "email1@gmail.com"}'}, 
             {'type': 'hr', 'data': '{"Name": "Name2","City": "city1", "EMail": "email2@gmail.com"}'},
             {'type': 'it', 'data': '{"Name": "Name3","City": "city3", "EMail": "email3@gmail.com"}'}, 
             {'type': 'it', 'data': '{"Name": "Name4","City": "city2", "EMail": "email4@gmail.com"}'}, 
             {'type': 'op', 'data': '{"Name": "Name5","City": "city1", "EMail": "email5@gmail.com"}'}]

Output:

[{'type': 'hr',
  'data': ['{"Name": "Name1","City": "city1", "EMail": "email1@gmail.com"}',
   '{"Name": "Name2","City": "city1", "EMail": "email2@gmail.com"}']},
 {'type': 'it',
  'data': ['{"Name": "Name3","City": "city3", "EMail": "email3@gmail.com"}',
   '{"Name": "Name4","City": "city2", "EMail": "email4@gmail.com"}']},
 {'type': 'op',
  'data': ['{"Name": "Name5","City": "city1", "EMail": "email5@gmail.com"}']}]

huangapple
  • 本文由 发表于 2023年6月15日 02:29:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定