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单元测试 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01