如何将多个键转换为单个字典并在 python 中创建每个键具有多个值的字典

How convert multiple keys into single of dictionary and create Dictionary with multiple values per key in python

我有嵌套的字典。像这样我从 Api 获取这些数据,这不过是 data_response:

    {
    "Parameter_1": {
        "2021-11-16 14:29:00": 319.56,
        "2021-11-16 15:16:00": 319.56,
        "2021-11-16 15:17:00": 319.56,
        "2021-11-17 00:00:00": 335.48,
        "2021-11-17 00:01:00": 335.48,
        "2021-11-17 00:02:00": 335.48,
        "2021-11-18 00:00:00": 355.45,
        "2021-11-18 00:01:00": 355.45,
        "2021-11-18 00:03:00": 355.45,
},
    "Parameter_2": {
        "2021-11-16 14:29:00": 319.56,
        "2021-11-16 15:16:00": 319.56,
        "2021-11-16 15:17:00": 319.56,
        "2021-11-17 00:00:00": 335.48,
        "2021-11-17 00:01:00": 335.48,
        "2021-11-17 00:02:00": 335.48,
         "2021-11-18 00:00:00": 355.45,
        "2021-11-18 00:01:00": 355.45,
        "2021-11-18 00:03:00": 355.45,
}

这是我做的代码:

data = []
for parameter in parameters:
    for key, values in data_response.items():
        for key1, value in values.items():
            date = key1.split(" ")[0]

    value_list = [value for key1, value in values.items()]
    data.append(value_list)

我在数据中得到的输出只是值:

数据:[[319.56,319.56, 319.56, 335.48,335.48,335.48,355.45,355.45,355.45]]

我希望获取键的值,只有一个日期。预期输出为:

预期输出 1:

{
    "Parameter_1": {
        "2021-11-16: [319.56,319.56,319.56],
        "2021-11-17": [335.48,335.48,335.48],
        "2021-11-18": [355.45,355.45,355.45],
},
    "Parameter_2": {
        "2021-11-16": [319.56,319.56, 319.56],
        "2021-11-17": [335.48,335.48,335.48],
        "2021-11-18": [355.45,355.45,355.45],
}

预期输出 2:

Date:
     [
      parameter1[2021-11-16][2021-11-17][2021-11-18],
      parameter2[2021-11-16][2021-11-17][2021-11-18]
     ]
Values:
     [
      parameter1[319.56,319.56,319.56][335.48,335.48,335.48][355.45,355.45,355.45],
      parameter2[319.56,319.56,319.56][335.48,335.48,335.48][355.45,355.45,355.45]
     ]

你能帮我实现我的输出吗?

我想这就是您要找的:

d = {
    "Parameter_1": {
        "2021-11-16 14:29:00": 319.56,
        "2021-11-16 15:16:00": 319.56,
        "2021-11-16 15:17:00": 319.56,
        "2021-11-17 00:00:00": 335.48,
        "2021-11-17 00:01:00": 335.48,
        "2021-11-17 00:02:00": 335.48,
        "2021-11-18 00:00:00": 355.45,
        "2021-11-18 00:01:00": 355.45,
        "2021-11-18 00:03:00": 355.45,
},
    "Parameter_2": {
        "2021-11-16 14:29:00": 319.56,
        "2021-11-16 15:16:00": 319.56,
        "2021-11-16 15:17:00": 319.56,
        "2021-11-17 00:00:00": 335.48,
        "2021-11-17 00:01:00": 335.48,
        "2021-11-17 00:02:00": 335.48,
         "2021-11-18 00:00:00": 355.45,
        "2021-11-18 00:01:00": 355.45,
        "2021-11-18 00:03:00": 355.45,
}}

r = dict()

for k, v in d.items():
    r[k] = dict()
    for k_, v_ in v.items():
        r[k].setdefault(k_[:10], []).append(v_)

print(r)

输出:

{'Parameter_1': {'2021-11-16': [319.56, 319.56, 319.56], '2021-11-17': [335.48, 335.48, 335.48], '2021-11-18': [355.45, 355.45, 355.45]}, 'Parameter_2': {'2021-11-16': [319.56, 319.56, 319.56], '2021-11-17': [335.48, 335.48, 335.48], '2021-11-18': [355.45, 355.45, 355.45]}}

这是非常干净的数据,Pandas 可以很好地处理它:

import pandas as pd

data = {
    "Parameter_1": {
        "2021-11-16 14:29:00": 319.56,
        "2021-11-16 15:16:00": 319.56,
        "2021-11-16 15:17:00": 319.56,
        "2021-11-17 00:00:00": 335.48,
        "2021-11-17 00:01:00": 335.48,
        "2021-11-17 00:02:00": 335.48,
        "2021-11-18 00:00:00": 355.45,
        "2021-11-18 00:01:00": 355.45,
        "2021-11-18 00:03:00": 355.45,
},
    "Parameter_2": {
        "2021-11-16 14:29:00": 319.56,
        "2021-11-16 15:16:00": 319.56,
        "2021-11-16 15:17:00": 319.56,
        "2021-11-17 00:00:00": 335.48,
        "2021-11-17 00:01:00": 335.48,
        "2021-11-17 00:02:00": 335.48,
         "2021-11-18 00:00:00": 355.45,
        "2021-11-18 00:01:00": 355.45,
        "2021-11-18 00:03:00": 355.45,
}

df = pd.DataFrame(data).reset_index()
df['index'] = pd.to_datetime(df['index']).dt.date.astype(str)
output = df.groupby('index').agg(list).to_dict()
print(output)

输出:(手动添加格式)

{
    'Parameter_1': {
        '2021-11-16': [319.56, 319.56, 319.56], 
        '2021-11-17': [335.48, 335.48, 335.48], 
        '2021-11-18': [355.45, 355.45, 355.45]
    },
    'Parameter_2': {
        '2021-11-16': [319.56, 319.56, 319.56], 
        '2021-11-17': [335.48, 335.48, 335.48], 
        '2021-11-18': [355.45, 355.45, 355.45]
    }
}
from typing import Dict, List

def output_expected_1(json: Dict[str, Dict[str, float]]) -> Dict[str, Dict[str, List[float]]]:
    """Parse the input json argument and return required output which is a
    dictionary with a label key and a value of a list of floats. The datatime string is 
    truncated from YYYY-MM-DD HH:MM:SS to YYYY-MM-DD (date).

    Given the arg json as:
    {
        "Parameter_1": {
            "2021-11-16 14:29:00": 319.56,
            "2021-11-16 15:16:00": 319.56,
            "2021-11-16 15:17:00": 319.56,
            "2021-11-17 00:00:00": 335.48,
            "2021-11-17 00:01:00": 335.48,
            "2021-11-17 00:02:00": 335.48,
            "2021-11-18 00:00:00": 355.45,
            "2021-11-18 00:01:00": 355.45,
            "2021-11-18 00:03:00": 355.45,
        },
        "Parameter_2": {
            "2021-11-16 14:29:00": 319.56,
            "2021-11-16 15:16:00": 319.56,
            "2021-11-16 15:17:00": 319.56,
            "2021-11-17 00:00:00": 335.48,
            "2021-11-17 00:01:00": 335.48,
            "2021-11-17 00:02:00": 335.48,
            "2021-11-18 00:00:00": 355.45,
            "2021-11-18 00:01:00": 355.45,
            "2021-11-18 00:03:00": 355.45,
        }
}



    Output would be:
    {
        'Parameter_1': {
            '2021-11-16': [319.56, 319.56, 319.56],
            '2021-11-17': [319.56, 319.56, 319.56],
        },

        'Parameter_2': {
            '2021-11-19': [319.56, 319.56, 319.56],
            '2021-11-22': [319.56, 319.56, 319.56],
        },
    }
    """
    output_dict = {}
    nested_dict = {}
    for label, content in json.items():
        for each_date_time, each_float in content.items():
            if not each_date_time[:10] in nested_dict.keys():
                nested_dict[each_date_time[:10]] = []
            nested_dict[each_date_time[:10]] = nested_dict[each_date_time[:10]].append(
                each_float) or nested_dict[each_date_time[:10]]

        output_dict[label] = nested_dict
        nested_dict = {}
    return output_dict