0

I have 7 dictionaries that I would like to merge one with the other:

The dictionaries have been tested individually and all seem to follow the same structure:

results1()=
{
 "NO4": 
  {
   "type_tests":
    {
     "test1":1.0,
     "test2":5.0,
     "test3":14.0
    }
  },
 "SO3": 
    {
     "type_tests":
    {"test1":1.0,
     "test2":5.0,
     "test3":14.0}
  }
}

results2()=
{
 "CO2": 
  {
   "type_tests":
    {
     "test1":10.0,
     "test2":51.0,
     "test3":34.0
    }
  },
 "H20": 
    {
     "type_tests":
    {"test1":1.0,
     "test2":5.0,
     "test3":14.0}
  }
}

I have tried to merge the 7 dictionaries following this post:

How to merge two dictionaries in a single expression?

in the way that what I have tried is merge the functions that output the dictionaries this way:

merge_dictionaries={**results1(),**results2(),**results3(),**results4(),**results5(),**results6(),**results7()}
return merge_dictionaries

however when I run merge dictionaries, it outputs:

TypeError: 'str' object is not a mapping

I have also seen this post merging "several" python dictionaries , and i tried :

dicts=[results1(),results2(),results3(),results4(),results5(),results6(),results7()]

for d in dicts:
    for k, v in d.iteritems():
        super_dict[k].add(v)
    return superdict

however outputs results7() dictionary and stays thinking (as if it was supposed to output something else but does not)

The desired output using the 2 dictionaries would be the dictionaries merged:

{
"NO4": 
    {
    "type_tests":
        {
         "test1":1.0,
         "test2":5.0,
         "test3":14.0
        }
     },
 "SO3": 
     {
    "type_tests":
        {
         "test1":1.0,
         "test2":5.0,
         "test3":14.0
        }
      },
"CO2": 
      {
     "type_tests":
        {
        "test1":10.0,
        "test2":51.0,
        "test3":34.0
      }
     },
 "H20": 
      {
      "type_tests":
       {
        "test1":1.0,
        "test2":5.0,
        "test3":14.0
    }
  }
}

how could I get a super dictionary merging the 7 individual dictionaries?

JamesHudson81
  • 2,215
  • 4
  • 23
  • 42
  • 1
    You `return` inside the innermost loop. Did you mean to `return` _after_ the outer loop instead? – tobias_k Aug 25 '17 at 12:33
  • What does `results1()` look like - what's that returning? – Jon Clements Aug 25 '17 at 12:41
  • Because if the first one is failing with an error - not sure how your second is passing... Also note that if all dictionaries are containing identical keys then you're only going to end up with the last value of each key... Are you trying to collect all values for the keys instead? – Jon Clements Aug 25 '17 at 12:43
  • What is desired output given a certain input? Do you want the values to be overwritten if duplicate key? Then your last solution is working as intented. If not, you have to check if key is set, if it is, turn its value into and array and add values to it. – mTv Aug 25 '17 at 12:48
  • tobias_k : After ammending return indentation accordingly, the output is the same, Jon Clements: no key is repeated at all, I am looking forward for the keys with their corresponding values in a json structure. mTv: the desired out is the same structured as the input but with all the dictionaries inside the same `json`. I have tested all the functions indepently and all of the output the same json structure. – JamesHudson81 Aug 25 '17 at 12:55

2 Answers2

0

I really don't know if I understand well your question, but this could be a solution:

dicts=[results1(),results2(),results3(),results4(),results5(),results6(),results7()]

merge_dictionaries = {k: v for d in dicts for k,v in d.items()}

Would be nice to know a clear and short but complete input and output example.

I guess that the more pythonic unpacking solution didn't work for you because you are using Python < 3.5.

  • Im actually using latest version of python. I tried you response but outputs `result7()` and then stays thinking as if It was going to output something else but it does not. I have added an input and output example hoping might get clearer – JamesHudson81 Aug 26 '17 at 09:51
  • @ge00rge just to be clear... Did you already have your problem solved with this solution? –  Aug 26 '17 at 13:59
  • yes , thanks to your response and changing the type of dicts from str to json, I could solve the problem. – JamesHudson81 Aug 26 '17 at 15:09
0

The functions that outputed the corresponding dictionaries, were type str therefore what I tried was to get each of the functions and turn them into jsons with the help of Ignacio Vazquez Abrams in his response in this post Convert string to JSON using Python :

format_results1=json.loads(results1())
format_results2=json.loads(results2())

After doing so I used @gsi-frank response, inserting in the dicts list the format_results . Finally I obtained the merged dictionaries.

In short , the problem was the type of the dictionaries , I turned them into json and then was able to merge them together.

Thank you everyone who took time reading my post and those who provide their answers.

JamesHudson81
  • 2,215
  • 4
  • 23
  • 42