-1

I have multiple dictionaries as follows:

{
    'Plant 1': {'min_cap': 100.0, 'max_cap': 2200.0,'Var_Cost': 0.75, 'Fix_Cost': 0.0029625000000000003},
    'Plant 2': {'min_cap': 50.0, 'max_cap': 300.0, 'Var_Cost': 0.4, 'Fix_Cost': 0.00058},
    'Plant 3': {'min_cap': 100.0, 'max_cap': 2500.0, 'Var_Cost': 1.83, 'Fix_Cost': -0.00178425},
    'Plant 4': {'min_cap': 100.0, 'max_cap': 4000.0, 'Var_Cost': 2.74, 'Fix_Cost': 0.012124500000000002}
}

I now want to combine multiple of these dictionaries (same structure, different values) into one of higher dimension. so that the resulting dictionary than looks like this:

{
'Group 1':{'Plant 1': {'min_cap': 100.0, 'max_cap': 2200.0,'Var_Cost': 0.75, 'Fix_Cost': 0.0029625000000000003},
           'Plant 2': {'min_cap': 50.0, 'max_cap': 300.0, 'Var_Cost': 0.4, 'Fix_Cost': 0.00058},
           'Plant 3': {'min_cap': 100.0, 'max_cap': 2500.0, 'Var_Cost': 1.83, 'Fix_Cost': -0.00178425},
           'Plant 4': {'min_cap': 100.0, 'max_cap': 4000.0, 'Var_Cost': 2.74, 'Fix_Cost': 0.012124500000000002}},

'Group 2':{'Plant 1': {'min_cap': 100.0, 'max_cap': 2200.0,'Var_Cost': 0.75, 'Fix_Cost': 0.0029625000000000003},
           'Plant 2': {'min_cap': 50.0, 'max_cap': 300.0, 'Var_Cost': 0.4, 'Fix_Cost': 0.00058},
           'Plant 3': {'min_cap': 100.0, 'max_cap': 2500.0, 'Var_Cost': 1.83, 'Fix_Cost': -0.00178425},
           'Plant 4': {'min_cap': 100.0, 'max_cap': 4000.0, 'Var_Cost': 2.74, 'Fix_Cost': 0.012124500000000002}},
....

'Group N':{'Plant 1': {'min_cap': 100.0, 'max_cap': 2200.0,'Var_Cost': 0.75, 'Fix_Cost': 0.0029625000000000003},
           'Plant 2': {'min_cap': 50.0, 'max_cap': 300.0, 'Var_Cost': 0.4, 'Fix_Cost': 0.00058},
           'Plant 3': {'min_cap': 100.0, 'max_cap': 2500.0, 'Var_Cost': 1.83, 'Fix_Cost': -0.00178425},
           'Plant 4': {'min_cap': 100.0, 'max_cap': 4000.0, 'Var_Cost': 2.74, 'Fix_Cost': 0.012124500000000002}}
}

I tried:

C = Group1,Group2

However that does not give me the name of each group in the above structure.

martineau
  • 119,623
  • 25
  • 170
  • 301
Perry Hall
  • 11
  • 5
  • Suggestion: use very simple dictionaries that have just a few elements and yet retain all the properties you're trying to account for. Then, provide the simple concatenation of these dictionaries. This makes it much easier to grasp what you need. – ggorlen Jan 10 '20 at 17:44
  • In the `C = Group1,Group2` what are `Group1` and `Group2`? How is this grouping being done? What determines how many groups there are? You need to [edit] and clarify your question. – martineau Jan 10 '20 at 19:04

2 Answers2

1

This is possible with an iteration (N times) in a dictionary comprehension:

d = {'Plant 1': {'min_cap': 100.0, 'max_cap': 2200.0,'Var_Cost': 0.75, 'Fix_Cost': 0.0029625000000000003}, 'Plant 2': {'min_cap': 50.0, 'max_cap': 300.0, 'Var_Cost': 0.4, 'Fix_Cost': 0.00058}, 'Plant 3': {'min_cap': 100.0, 'max_cap': 2500.0, 'Var_Cost': 1.83, 'Fix_Cost': -0.00178425}, 'Plant 4': {'min_cap': 100.0, 'max_cap': 4000.0, 'Var_Cost': 2.74, 'Fix_Cost': 0.012124500000000002}}

N = 4       # N is 4 here as an example
groups = {f'Group {i}': d for i in range(1, N+1)}
Austin
  • 25,759
  • 4
  • 25
  • 48
  • thanks for this. it set me up a good track. when i used the code as is, it was reading 4 times the same dictionary, however i have multiple different groups (i.e. same structure but different data). i have now used the following amendment: `groups = {f'Group {i}': globals()["group"+str(i)] for i in range(1, N+1)}` ...this does exactly what i wanted. is there any issue with using `globals()`? i read things like never do use `globals()`. – Perry Hall Jan 11 '20 at 11:04
0

I think you just want a new dictionary, where the new keys are "group %d" for each one. Are the contents identical?

If your original dict is called plant_dict, then what you want is something like:

group_dict = { f'group {idict+1}': plant_dict.copy() for idict in range(n) }

Note the +1 since your numbering starts with 1 not 0, and the copy so that each one is actually a separate dict -- probably what you want.

If you actually have some array of plant_dicts (call it plant_dicts), then you can do

group_dict = { f'group {idict+1}': pd for idict, pd in enumerate(plant_dicts) }

These are dictionary comprehensions which make new dictionaries with keys and values given by what is on either side of the :.

Andrew Jaffe
  • 26,554
  • 4
  • 50
  • 59
  • @Austin, fixed the starting-from-1 issue, but I think the second one is now correct. (Not sure if you saw it before a syntax correction to fix a repeated `idict`). – Andrew Jaffe Jan 10 '20 at 17:58
  • @Austin, yes, I am assuming that `plant_dicts` is the array of individual `plant_dict`-like entries. – Andrew Jaffe Jan 10 '20 at 18:00
  • Thanks for looking into this. actually i only have individual dictionaries `group1`, `group2`, ....`groupN` that I want to aggregate, so that I can access the variables per group for optimisation analysis further down in the code. So I struggled a bit to make the above code work for my case, as i do not have an aggregate array to start with. I have now used: `groups = {f'Group {i}': globals()["group"+str(i)] for i in range(1, N+1)}`. which on visual inspection `print(groups)` seems to produce what I need. – Perry Hall Jan 11 '20 at 11:12
  • Thanks, @PerryHall, but perhaps you should consider the code where you create the `groupn` variables and see if you can change it there to create a list of groups to begin with, and then you also won't have to muck around with `globals` which is inelegant and unpythonic. Also, please upvote answers you like! – Andrew Jaffe Jan 11 '20 at 11:34
  • Thanks Andrew. i have upvoted, but unfortunately it will not be shown, as i am still a rookie on there. need to collect 6 more reputation points. Regarding getting rid of the global variable. I will make a note, so can come back to it when the general data flow in my script works. This is just reading data and grouping it for subsequent calculation, which also will only read from the dictionary. So the "mega"-dictionary created here, will only be read and never written to, other than during creation. – Perry Hall Jan 11 '20 at 12:54