合并字典中的值并重命名键



对于下面的字典,我需要重新构造它,以便有一个包含用户条目的新字典,另一个条目列出了除该用户和时间外请求的所有消息。我使用的字典是:

dictio =   { "Items": [{ "user": "a@gmail.com",
"type": "Product Team", 
"message": "Developer",
"employeeId": "101",
"message_requested": "Requested for the 192.168.1.1 access", 
"Time": "2021-01-08 12:09:54.986542" }, 
{ "user": "a@gmail.com",
"type": "Product Team",
"message": "Developer",
"employeeId": "101",
"message_requested": "Requested for the 192.168.1.2 access",
"Time": "2021-01-09 12:10:54.986542" }],
"Count": 2, 
"ScannedCount": 5, 
"ResponseMetadata": {"RequestId": "xx",
"HTTPStatusCode": 200,
"HTTPHeaders": { "server": "Server",
"date": "Fri, 22 Jan 2021 08:02:13 GMT", 
"content-type": "application/x-amz-json-1.0", 
"content-length": "3533", 
"connection": "keep-alive", 
"x-amzn-requestid": "xx", 
"x-amz-crc32": "xx" }, 
"RetryAttempts": 0 } }

My expected out is

{"user": "a@gmail.com", 
"message_requested_Time":[{"message_requested": "Requested for the 192.168.1.1 access", 
"Time": "2021-01-08 12:09:54.986542"},
{"message_requested": "Requested for the 192.168.1.2 access", 
"Time": "2021-01-09 12:10:54.986542" }]}

代码在

下面
super_dict = {"user":'', "mesage_requested_Time":[]}
for d in dictio:
for l, m in d.items():  
super_dict.setdefault(l, []).append(m)
super_dict

另一个用于测试的字典

dictio={ "Items": [ { "user": "a@gmail.com", 
"type": "Product Team",
"message": "Developer", 
"employeeId": "101",
"message_requested": "Requested for the 192.168.1.1 access", 
"Time": "2021-01-08 12:09:54.986542" }, 
{ "user": "a@gmail.com",
"type": "Product Team",
"message": "Developer",
"employeeId": "101", 
"message_requested": "Requested for the 192.168.1.2 access", 
"Time": "2021-01-09 12:10:54.986542" },
{ "user": "a@gmail.com",
"type": "Ops",
"message": "Developer",
"employeeId": "101",
"message_requested": "Requested for the 192.168.1.2 access", 
"Time": "2021-01-09 12:10:54.986542" } ],
"Count": 2,
"ScannedCount": 5, 
"ResponseMetadata": { "RequestId": "xx",
"HTTPStatusCode": 200,
"HTTPHeaders": {"server": "Server",
"date": "Fri, 22 Jan 2021 08:02:13 GMT", 
"content-type": "application/x-amz-json-1.0", 
"content-length": "3533", 
"connection": "keep-alive", 
"x-amzn-requestid": "xx",
"x-amz-crc32": "xx" }, 
"RetryAttempts": 0 } }```
dictio =   { "Items": [ { "user": "a@gmail.com", "type": "Product Team", "message": "Developer", "employeeId": "101", "message_requested": "Requested for the 192.168.1.1 access", "Time": "2021-01-08 12:09:54.986542" }, { "user": "a@gmail.com", "type": "Product Team", "message": "Developer", "employeeId": "101", "message_requested": "Requested for the 192.168.1.2 access", "Time": "2021-01-09 12:10:54.986542" } ], "Count": 2, "ScannedCount": 5, "ResponseMetadata": { "RequestId": "xx", "HTTPStatusCode": 200, "HTTPHeaders": { "server": "Server", "date": "Fri, 22 Jan 2021 08:02:13 GMT", "content-type": "application/x-amz-json-1.0", "content-length": "3533", "connection": "keep-alive", "x-amzn-requestid": "xx", "x-amz-crc32": "xx" }, "RetryAttempts": 0 } }
res = {}
for k,v in dictio.items():
if k == 'Items':
for i in v:
res['user'] = i['user']
res.setdefault('mesage_requested_Time', []).append({'message_requested' : i['message_requested'], 'Time' : i['Time']})
print(res)

输出
{'user': 'a@gmail.com', 'mesage_requested_Time': [{'message_requested': 'Requested for the 192.168.1.1 access', 'Time': '2021-01-08 12:09:54.986542'}, {'message_requested': 'Requested for the 192.168.1.2 access', 'Time': '2021-01-09 12:10:54.986542'}]}

更快更python化:

from operator import itemgetter
from itertools import groupby
for user, infos in groupby(dictio2['Items'], key=itemgetter('user')):
output = {'user': user,
'message_requested_Time': [{key: val for key, val in info.items() if key in ['message_requested', 'Time']}
for info in infos]}

您可以避免导入itemgetter并使用lambda表达式,如:

lambda x: x['user']

但是itemgetter函数稍微快一点。

相关内容

  • 没有找到相关文章

最新更新