Home > Mobile >  Convert dictionary to dictionary of lists by using strings
Convert dictionary to dictionary of lists by using strings

Time:05-12

I have data in the following form:

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

I need somehow to modify the above form in the following form:

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

Specifically, I want to get the variables HVAC_flex_available and DHW_flex_available and keep the numbers after the indices (1,1), (2,1), etc.

Any idea of how can I implement it?

EDIT: What I have tried until now is the following:

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

But I am getting:

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

CodePudding user response:

You can achieve this with dictionary comprehension, considering the exact format of this input dictionary :

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

CodePudding user response:

The dictionary you are looking for will be in 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()]
}

CodePudding user response:

A fairly straightforward dictionary comprehension should do it:

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)

Output:

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

CodePudding user response:

Previous replies already mentioned dictionary comprehension, but here is a little different approach.

I made sure to include the wanted_keys variable so that you can easily modify the keys that you would like to fetch.

Assigning your dictionary to a your_dict variable should do the trick.

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

CodePudding user response:

You can try something like this. In the below code d1 is the dict variable 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)

Result

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