字典字典的元组列表
list of tuples to dictionary of dictionaries
我正在以 list of tuples
的形式从分层数据库中检索 (root, parent1, parent2, child1)
,如:
[('HCS', 'Assured Build', 'Implementation', 'Hardware Stack'),
('HCS', 'Assured Build', 'Implementation', 'SA and SF'),
('HCS', 'Assured Build', 'Testing and Validation', 'NFRU-SS'),
('HCS', 'Assured Build', 'Testing and Validation', 'NRFU-UC'),
('HCS', 'Assured Platform', 'Restoration', 'AS Build'),
('HCS', 'Assured Platform', 'Restoration', 'Capacity Management'),
('HCS', 'Assured Platform', 'Migration', 'Document Review')]
我想创建一个字典的字典来轻松迭代并创建一个树视图:
{"HCS":
{"Assured Build":
{"Implementation":{"Hardware Stack", "Software"},
{"Testing and Validation":{"NRFU-SS", "NRFU-UC"}
},
{"Assured Platform":
{"Restoration":{"AS Build","Capacity Management"},
{"Migration":{"Document Review"}},
}
}
处理此问题的最佳方法是什么?我试过 namedtuple 和 defaultdict 都失败了。
您需要 defaultdict
或 defaultdict
或 defaultdict
或 list
(或 set
,如果需要):
import json
from collections import defaultdict
l = [('HCS', 'Assured Build', 'Implementation', 'Hardware Stack'),
('HCS', 'Assured Build', 'Implementation', 'SA and SF'),
('HCS', 'Assured Build', 'Testing and Validation', 'NFRU-SS'),
('HCS', 'Assured Build', 'Testing and Validation', 'NRFU-UC'),
('HCS', 'Assured Platform', 'Restoration', 'AS Build'),
('HCS', 'Assured Platform', 'Restoration', 'Capacity Management'),
('HCS', 'Assured Platform', 'Migration', 'Document Review')]
d = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
for key1, key2, key3, value in l:
d[key1][key2][key3].append(value)
print(json.dumps(d, indent=4))
json.dumps()
这里只是为了一个漂亮的打印。它打印:
{
"HCS": {
"Assured Platform": {
"Restoration": [
"AS Build",
"Capacity Management"
],
"Migration": [
"Document Review"
]
},
"Assured Build": {
"Implementation": [
"Hardware Stack",
"SA and SF"
],
"Testing and Validation": [
"NFRU-SS",
"NRFU-UC"
]
}
}
}
我们还可以使嵌套的 defauldict
初始化步骤更加 generic 和 extract that into a reusable method:
def make_defaultdict(depth, data_structure):
d = defaultdict(data_structure)
for _ in range(depth):
d = defaultdict(lambda d=d: d)
return d
然后,您可以替换:
d = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
与:
d = make_defaultdict(2, list)
我正在以 list of tuples
的形式从分层数据库中检索 (root, parent1, parent2, child1)
,如:
[('HCS', 'Assured Build', 'Implementation', 'Hardware Stack'),
('HCS', 'Assured Build', 'Implementation', 'SA and SF'),
('HCS', 'Assured Build', 'Testing and Validation', 'NFRU-SS'),
('HCS', 'Assured Build', 'Testing and Validation', 'NRFU-UC'),
('HCS', 'Assured Platform', 'Restoration', 'AS Build'),
('HCS', 'Assured Platform', 'Restoration', 'Capacity Management'),
('HCS', 'Assured Platform', 'Migration', 'Document Review')]
我想创建一个字典的字典来轻松迭代并创建一个树视图:
{"HCS":
{"Assured Build":
{"Implementation":{"Hardware Stack", "Software"},
{"Testing and Validation":{"NRFU-SS", "NRFU-UC"}
},
{"Assured Platform":
{"Restoration":{"AS Build","Capacity Management"},
{"Migration":{"Document Review"}},
}
}
处理此问题的最佳方法是什么?我试过 namedtuple 和 defaultdict 都失败了。
您需要 defaultdict
或 defaultdict
或 defaultdict
或 list
(或 set
,如果需要):
import json
from collections import defaultdict
l = [('HCS', 'Assured Build', 'Implementation', 'Hardware Stack'),
('HCS', 'Assured Build', 'Implementation', 'SA and SF'),
('HCS', 'Assured Build', 'Testing and Validation', 'NFRU-SS'),
('HCS', 'Assured Build', 'Testing and Validation', 'NRFU-UC'),
('HCS', 'Assured Platform', 'Restoration', 'AS Build'),
('HCS', 'Assured Platform', 'Restoration', 'Capacity Management'),
('HCS', 'Assured Platform', 'Migration', 'Document Review')]
d = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
for key1, key2, key3, value in l:
d[key1][key2][key3].append(value)
print(json.dumps(d, indent=4))
json.dumps()
这里只是为了一个漂亮的打印。它打印:
{
"HCS": {
"Assured Platform": {
"Restoration": [
"AS Build",
"Capacity Management"
],
"Migration": [
"Document Review"
]
},
"Assured Build": {
"Implementation": [
"Hardware Stack",
"SA and SF"
],
"Testing and Validation": [
"NFRU-SS",
"NRFU-UC"
]
}
}
}
我们还可以使嵌套的 defauldict
初始化步骤更加 generic 和 extract that into a reusable method:
def make_defaultdict(depth, data_structure):
d = defaultdict(data_structure)
for _ in range(depth):
d = defaultdict(lambda d=d: d)
return d
然后,您可以替换:
d = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
与:
d = make_defaultdict(2, list)