字典列表,在字典中 - 在 Python 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1889385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:17:12  来源:igfitidea点击:

List of dictionaries, in a dictionary - in Python

pythonlistdictionarynested

提问by Terry Felkrow

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!

上面的代码显然不起作用,因为我不知道该怎么做!

回答by SilentGhost

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.rulesis a dictionary of a list of a dictionary of a dictionary. I bet there is more sane way to do this.

这种方式self.rules是字典的字典列表的字典。我敢打赌有更明智的方法来做到这一点。

回答by Arthur Thomas

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.

继续添加到结构中。

回答by Terry Felkrow

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. (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]

回答by Dave Webb

Your example code doesn't seem to be valid Python. It's not clear if the second level element is supposed to be a list or a dictionary.

您的示例代码似乎不是有效的 Python。不清楚第二级元素应该是列表还是字典。

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

回答by Pete

My favorite way to deal with nested dictionaries & lists of dictionaries is to use PyYAML. See this response for details.

我最喜欢处理嵌套字典和字典列表的方法是使用PyYAML有关详细信息,请参阅此回复