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 中
基础教程推荐
- Discord.py 缺少必需的参数 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
