简体   繁体   English

字典中的字典列表-Python

[英]List of dictionaries, in a dictionary - in Python

I have a case where I need to construct following structure programmatically (yes I am aware of .setdefault and defaultdict but I can not get what I want) 我有一种情况,我需要以编程方式构造以下结构(是的,我知道.setdefault和defaultdict,但我无法得到想要的东西)

I basically need a dictionary, with a dictionary of dictionaries created within the loop. 我基本上需要一个字典,并在循环内创建字典字典。 At the beginning the structure is completely blank. 开始时,结构完全空白。

structure sample (please note, I want to create an array that has this structure in the code!) 结构样本(请注意,我想创建一个在代码中具有此结构的数组!)

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

pseudo code that needs to 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 is obviously not working since I dont know how to do it! 上面的代码显然不起作用,因为我不知道该怎么做!

Example, that you've posted is not a valid python code, I could only imagine that you're trying to do something like this: 例如,您发布的不是有效的python代码,我只能想象您正在尝试执行以下操作:

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

this way self.rules is a dictionary of a list of a dictionary of a dictionary. 这样, self.rules是字典的字典列表的字典。 I bet there is more sane way to do this. 我敢打赌,还有更明智的方法可以做到这一点。

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 the 'f_expr' and 'c_expr' since you never mention where you get those but I assume they are already know column names in a resultset or structure of some sort. 我不确定该怎么称呼“ f_expr”和“ c_expr”,因为您从不提及从何处获得这些信息,但我认为它们已经知道某种结果集或某种结构中的列名。

Just keep adding to the structure as you go. 只要继续添加结构即可。

Your example code doesn't seem to be valid Python. 您的示例代码似乎不是有效的Python。 It's not clear if the second level element is supposed to be a list or a dictionary. 目前尚不清楚第二级元素应该是列表还是字典。

However, if you're doing what I think you're doing, and it's a dictionary, you could use a tuple as a key in the top-level dictionary instead of nesting 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'}}

My favorite way to deal with nested dictionaries & lists of dictionaries is to use PyYAML . 我最喜欢的处理嵌套词典和词典列表的方法是使用PyYAML See this response for details . 有关详细信息,请参见此响应

Well, I apologize for the confusion, I never claimed that code actually compiled, hence (pseudo). 好吧,我为此感到抱歉,我从未声称代码实际上已经过编译,因此(伪)。 Arthur Thomas put me on the right track, here is slightly modified version. 亚瑟·托马斯(Arthur Thomas)使我走上了正确的轨道,这里是经过稍微修改的版本。 (Yes, now its a simply nested dictionary, 3 levels down) (是的,现在它是一个简单的嵌套字典,向下3级)

    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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM