使用字符串将字典转换为列表字典

Convert dictionary to dictionary of lists by using strings

我有以下形式的数据:

{None: {'VPP_users': {None: [1, 2, 3, 4]}, 'timesteps': {None: [1, 2]}, 'DR_signal': {None: 180.0}, 'power_contract': {(1, 1): 25, (2, 1): 78, (3, 1): 95, (4, 1): 9, (1, 2): 91, (2, 2): 62, (3, 2): 92, (4, 2): 52}, 'HVAC_flex_available': {(1, 1): 5, (2, 1): 24, (3, 1): 28, (4, 1): 9, (1, 2): 14, (2, 2): 25, (3, 2): 10, (4, 2): 50}, 'DHW_flex_available': {(1, 1): 46, (2, 1): 27, (3, 1): 27, (4, 1): 6, (1, 2): 10, (2, 2): 10, (3, 2): 27, (4, 2): 22}}}

我需要以某种方式将上面的表格修改为以下形式:

{'HVAC_flex_available': [5, 24, 28, 9, 14, 25, 10, 50], 'DHW_flex_available': [46, 27, 27, 6, 10, 10, 27, 22]}

具体来说,我想获取变量 HVAC_flex_availableDHW_flex_available 并将数字保留在索引 (1,1)(2,1)

之后

知道如何实施吗?

编辑:到目前为止我尝试过的是:

prefix = ["HVAC_flex_available", "DHW_flex_Avaliable"] 
output = {b: [v for key,v in solution.items() if key.startswith(b)] for b in prefix 

但我得到:

AttributeError: 'NoneType' object has no attribute 'startswith'

考虑到此输入字典的确切格式,您可以通过字典理解来实现此目的:

d = {None: {'VPP_users': {None: [1, 2, 3, 4]}, 'timesteps': {None: [1, 2]}, 'DR_signal': {None: 180.0}, 'power_contract': {(1, 1): 25, (2, 1): 78, (3, 1): 95, (4, 1): 9, (1, 2): 91, (2, 2): 62, (3, 2): 92, (4, 2): 52}, 'HVAC_flex_available': {(1, 1): 5, (2, 1): 24, (3, 1): 28, (4, 1): 9, (1, 2): 14, (2, 2): 25, (3, 2): 10, (4, 2): 50}, 'DHW_flex_available': {(1, 1): 46, (2, 1): 27, (3, 1): 27, (4, 1): 6, (1, 2): 10, (2, 2): 10, (3, 2): 27, (4, 2): 22}}}
d2 = {key:[v for v in d[None][key].values()] for key in ['HVAC_flex_available', 'DHW_flex_available']}

# Output
{'HVAC_flex_available': [5, 24, 28, 9, 14, 25, 10, 50], 'DHW_flex_available': [46, 27, 27, 6, 10, 10, 27, 22]}

您要查找的字典将在data_new

data = {None: {'VPP_users': {None: [1, 2, 3, 4]}, 'timesteps': {None: [1, 2]}, 'DR_signal': {None: 180.0}, 'power_contract': {(1, 1): 25, (2, 1): 78, (3, 1): 95, (4, 1): 9, (1, 2): 91, (2, 2): 62, (3, 2): 92, (4, 2): 52}, 'HVAC_flex_available': {(1, 1): 5, (2, 1): 24, (3, 1): 28, (4, 1): 9, (1, 2): 14, (2, 2): 25, (3, 2): 10, (4, 2): 50}, 'DHW_flex_available': {(1, 1): 46, (2, 1): 27, (3, 1): 27, (4, 1): 6, (1, 2): 10, (2, 2): 10, (3, 2): 27, (4, 2): 22}}}


data_new = {
    'HVAC_flex_available' : [v for k,v in data[None]['HVAC_flex_available'].items()],
    'DHW_flex_available' : [v for k,v in data[None]['DHW_flex_available'].items()]
}

你可以尝试这样的事情。在下面的代码中 d1 是 dict 变量 ie

d1 = {None: {'VPP_users': {None: [1, 2, 3, 4]}, 'timesteps': {None: [1, 2]}, 'DR_signal': {None: 180.0}, 'power_contract': {(1, 1): 25, (2, 1): 78, (3, 1): 95, (4, 1): 9, (1, 2): 91, (2, 2): 62, (3, 2): 92, (4, 2): 52}, 'HVAC_flex_available': {(1, 1): 5, (2, 1): 24, (3, 1): 28, (4, 1): 9, (1, 2): 14, (2, 2): 25, (3, 2): 10, (4, 2): 50}, 'DHW_flex_available': {(1, 1): 46, (2, 1): 27, (3, 1): 27, (4, 1): 6, (1, 2): 10, (2, 2): 10, (3, 2): 27, (4, 2): 22}}}
d = d1[None]
v1 = None
v2 = None
for key in d:
    if key == 'HVAC_flex_available':
        v1 = [item for item in d[key].values()]
    if key == 'DHW_flex_available':
        v2 = [item for item in d[key].values()]

result = {
    'HVAC_flex_available': v1,
    'DHW_flex_available': v2
}

print(result)

结果

{'HVAC_flex_available': [5, 24, 28, 9, 14, 25, 10, 50], 'DHW_flex_available': [46, 27, 27, 6, 10, 10, 27, 22]}

一个相当简单的字典理解应该可以做到:

d_in = {None: {'VPP_users': {None: [1, 2, 3, 4]}, 'timesteps': {None: [1, 2]}, 'DR_signal': {None: 180.0}, 'power_contract': {(1, 1): 25, (2, 1): 78, (3, 1): 95, (4, 1): 9, (1, 2): 91, (2, 2): 62, (3, 2): 92, (4, 2): 52}, 'HVAC_flex_available': {(1, 1): 5, (2, 1): 24, (3, 1): 28, (4, 1): 9, (1, 2): 14, (2, 2): 25, (3, 2): 10, (4, 2): 50}, 'DHW_flex_available': {(1, 1): 46, (2, 1): 27, (3, 1): 27, (4, 1): 6, (1, 2): 10, (2, 2): 10, (3, 2): 27, (4, 2): 22}}}

d_out = {key: list(d_in[None][key].values()) for key in ('HVAC_flex_available', 'DHW_flex_available')}

print(d_out)

输出:

{'HVAC_flex_available': [5, 24, 28, 9, 14, 25, 10, 50], 'DHW_flex_available': [46, 27, 27, 6, 10, 10, 27, 22]}

之前的回帖已经提到了字典理解,但这里有一点不同的做法。

我确保包含 wanted_keys 变量,以便您可以轻松修改要获取的密钥。

将字典分配给 your_dict 变量应该可以解决问题。

# Creating a tuple consists of keys which we would like to fetch data from
wanted_keys = ('HVAC_flex_available', 'DHW_flex_available',)

# For each key in wanted keys, the key and values of the nested key are stored in a dictionary
output_dict = {k: list(your_dict.get(None, {}).get(k, {}).values()) for k in wanted_keys}