如何在长度未知的嵌套字典中提取密钥,并将该密钥与遍历路径相等,并将它们作为键值对存储在另一个字典中。我有一个嵌套字典如下
{
"ingestion_config": {
"location": {},
"start_sequence": {},
"datafeed": {
"t04047": {
"validation": {
"triple_check": {},
"record_count_validation": {}
},
"date_pattern": {},
"cdc_config": {}
}
}
}
}
我希望在不同的层次上获取密钥,并将其等同于下面的遍历路径
{
ingestion_config: [ingestion_config]
location: [ingestion_config][location],
start_sequence: [ingestion_config][start_sequence],
datafeed: [ingestion_config][datafeed]
t04047: [ingestion_config][datafeed][t04047]
triple_check: [ingestion_config][data_feed][t04047][validation][trip_check]
}
我找到的与我类似的场景最接近的帖子是: here
可以对生成器使用递归。在函数的签名中,保留了一个默认参数,用于跟踪递归在每次调用
paths
时通过列表串联累积的路径:输出:
相关问题 更多 >
编程相关推荐