List of dictionaries, in a dictionary - in 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 中
基础教程推荐
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
