1

以下の辞書を考えると、ユーザーのエントリと、そのユーザーが要求したすべてのメッセージと時間をリストする別のエントリを含む新しい辞書を作成するために、辞書を再構築する必要があります。私が使用している口述はこれです:

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

私の予想アウトは

{"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 } }```
4

2 に答える 2