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

List of dictionaries, in a dictionary - in Python(字典列表,在字典中 - 在 Python 中)

本文介绍了字典列表,在字典中 - 在 Python 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个案例,我需要以编程方式构建以下结构(是的,我知道 .setdefault 和 defaultdict,但我无法得到我想要的)

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)

我基本上需要一个字典,在循环中创建一个字典.一开始结构完全空白.

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!

推荐答案

例如,您发布的不是有效的 Python 代码,我只能想象您正在尝试执行以下操作:

Example, that you've posted is not a valid python code, I could only imagine that you're trying to do something like this:

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

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

this way self.rules is a dictionary of a list of a dictionary of a dictionary. I bet there is more sane way to do this.

这篇关于字典列表,在字典中 - 在 Python 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:字典列表,在字典中 - 在 Python 中

基础教程推荐