List of dictionaries, in dictionary - in Python

I have a case where I need to build the following structure programmatically (yes, I know .setdefault and defaultdict, but I can't get what I want)

I basically need a dictionary with a dictionary of dictionaries created in a loop. The structure is completely empty at first.

sample structure (note that I want to create an array that has this structure in code!)

RULE = {
     'hard_failure': {
        4514 : {
           'f_expr' = 'ABC',
           'c_expr' = 'XF0',
     }
    }
   }

      

pseudo code that should create this:

...
self.rules = {}
for row in rows:
     a = 'hard_failure'
     b = row[0] # 4514
     c = row[1] # ABC
     d = row[2] # XF0
     # Universe collapse right after
     self.rules = ????
...   

      

The code above obviously doesn't work as I don't know how to do it!

+2


source to share


5 answers


The example you posted is not valid Python code, I could only imagine that you are trying to do something like this:

self.rules[a] = [{b:{'f_expr': c, 'c_expr': d}}]

      



Thus, self.rules

is a dictionary of a list of a dictionary of a dictionary. I bet there is a smarter way to do this.

+5


source


rules = {}
failure = 'hard_failure'
rules[failure] = []
for row in rows:
  #this is what people are referring to below.  You left out the addition of the    dictionary structure to the list.
  rules[failure][row[0]] = {} 
  rules[failure][row[0]]['type 1'] = row[1]
  rules[failure][row[0]]['type 2'] = row[2]

      

This is what I created based on how I understood the questions. I wasn't sure what to call "f_expr" and "c_expr" since you never mention where you got them, but I assume they already know the column names in the result set or structure of some kind.



Just keep adding to the structure as you go.

+1


source


Your example code is not valid Python. It is not clear if the second level item is a list or a dictionary.

However, if you are doing what I think you are doing and it is a dictionary, you can use a tuple as a key in the top-level dictionary instead of nested dictionaries:

>>> a = 'hard_failure'
>>> b = 4514
>>> c = "ABC"
>>> d = "XF0"
>>> rules = {}
>>> rules[(a,b)] = {'f_expr' : a,'c_expr' : d}
>>> rules
{('hard_failure', 4514): {'c_expr': 'XF0', 'f_expr': 'hard_failure'}}

      

0


source


My favorite way of working with nested dictionaries and lists of dictionaries is using PyYAML . See this answer for details .

0


source


Ok, I apologize for the confusion, I never claimed that the code is actually compiled, hence (pseudo). Arthur Thomas put me on the right track, here's a slightly modified version. (Yes, now its just a nested dictionary, 3 levels down)

    RULE_k = 'hard_failure'
    self.rules = {}
    for row in rows:
           self.rules_compiled.setdefault(RULE_k, {})
           self.rules_compiled[RULE_k][row[1]] = {}
           self.rules_compiled[RULE_k][row[1]]['f_expr'] = row[0]
           self.rules_compiled[RULE_k][row[1]]['c_expr'] = row[1]

      

0


source







All Articles