Python中的词典列表 [英] List of dictionaries, in a dictionary - in 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.
结构示例(请注意,我想创建一个在代码中具有此结构的数组) / p>
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',
}
}
}
需要创建的伪代码:
...
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中的词典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!