gpt4 book ai didi

json - 将嵌套字典合并到 d3 sunburst 的 Json 文件

转载 作者:行者123 更新时间:2023-12-01 00:17:12 24 4
gpt4 key购买 nike

我遇到的问题是我需要一个重嵌套字典的列表来合并成一个字典。所有的小字典都有完全相同的布局

{
"name": "root",
"tax": "Tax level: domain",
"children": [
{
"name": "Bacteria",
"tax": "Tax level: Kingdom",
"children": [
{
"name": "Firmicutes",
"tax": "Tax level: Phylum",
"children": [
{
"name": "Bacillidae",
"tax": "Tax level: Class",
"children": [
{
"name": "Bacillinae",
"tax": "Tax level: Order",
"children": [
{
"name": "Bacillini",
"tax": "Tax level: Family",
"children": [
{
"name": "Bacillus",
"tax": "Tax level: Genus",
"children": [
{
"name": "",
"size": 5,
"tax": "Tax level: Species"
}
]
}
]
}
]
}
]
}
]
}
]
}
]}

不同的解决方案尝试只合并第一个字典; "name": "root"得到了这个结果:
{
"name": "root",
"tax": "Tax level: domain",
"children": [
{
"name": "Bacteria",
"tax": "Tax level: Kingdom",
"children": [
{
"name": "Firmicutes",
"tax": "Tax level: Phylum",
"children": [
{
"name": "Bacillidae",
"tax": "Tax level: Class",
"children": [
{
"name": "Bacillinae",
"tax": "Tax level: Order",
"children": [
{
"name": "Bacillini",
"tax": "Tax level: Family",
"children": [
{
"name": "Bacillus",
"tax": "Tax level: Genus",
"children": [
{
"name": "",
"size": 5,
"tax": "Tax level: Species"
}
]
}
]
}
]
}
]
}
]
}
]
},
{
"name": "Bacteria",
"tax": "Tax level: Kingdom",
"children": [
{
"name": "Thermotogae",
"tax": "Tax level: Phylum",
"children": [
{
"name": "Thermotogae",
"tax": "Tax level: Class",
"children": [
{
"name": "Thermotogales",
"tax": "Tax level: Order",
"children": [
{
"name": "Thermotogaceae",
"tax": "Tax level: Family",
"children": [
{
"name": "Thermotoga",
"tax": "Tax level: Genus",
"children": [
{
"name": "",
"size": 5,
"tax": "Tax level: Species"
}
]
}
]
}
]
}
]
}
]
}
]
}

但我需要的是要删除的重复项和要附加的其他行。如下所示(手动完成):
{
"name": "root",
"tax": "Tax level: domain",
"children": [
{
"name": "Bacteria",
"tax": "Tax level: Kingdom",
"children": [
{
"name": "Firmicutes",
"tax": "Tax level: Phylum",
"children": [
{
"name": "Bacillidae",
"tax": "Tax level: Class",
"children": [
{
"name": "Bacillinae",
"tax": "Tax level: Order",
"children": [
{
"name": "Bacillini",
"tax": "Tax level: Family",
"children": [
{
"name": "Bacillus",
"tax": "Tax level: Genus",
"children": [
{
"name": "",
"size": 5,
"tax": "Tax level: Species"
}
]
}
]
}
]
},
{
"name": "Firmicutes",
"tax": "Tax level: Class",
"children": [
{
"name": "Tissierellia",
"tax": "Tax level: Order",
"children": [
{
"name": "unclassified Tissierellia",
"tax": "Tax level: Family",
"children": [
{
"name": "Tepidimicrobium",
"tax": "Tax level: Genus",
"children": [
{
"name": "",
"size": 5,
"tax": "Tax level: Species"
}
]
}
]
}
]
}
]
}
]
}
]
},
{
"name": "Thermotogae",
"tax": "Tax level: Phylum",
"children": [
{
"name": "Thermotogae",
"tax": "Tax level: Class",
"children": [
{
"name": "Thermotogales",
"tax": "Tax level: Order",
"children": [
{
"name": "Thermotogaceae",
"tax": "Tax level: Family",
"children": [
{
"name": "Thermotoga",
"tax": "Tax level: Genus",
"children": [
{
"name": "",
"size": 5,
"tax": "Tax level: Species"
}
]
}
]
}
]
}
]
}
]
}
]
}
]}

我编写的代码接受一个字典列表并将它们一一发送到合并字典的合并模块。
tempCount = -1                                                                      #initializes Counter for dictionary, starts at -1 to compensate index errors.
try:
for i in tempDicts: #Loops over list with small dictionarys
tempCount += 1 #Adds to the counter
if tempCount <=0: #if the counter is 0, it adds the first and second dictionary to a merged dictionary
result = merge(tempDicts[tempCount], tempDicts[tempCount+1])
elif tempCount > 0: #if the counter is higher than 0, it merges the small dictionary to the already merged dictionary.
result = merge(result, tempDicts[tempCount+1])
else:
print("Error in merging temporary dictionarys!") #catches random errors.
print("test")
print(result)
print(reduce(result))

except IndexError: #catches index out of range errors.
print("Finished Merging") #if there are no more small dicts to merge, it gets an error and stops.

最佳答案

RFC 7386 ,我们有 JSON 合并。
并且只需使用数组的特殊过程扩展 JSON 合并。

def merge_patch(target, patch, arraykey="name"):
if isinstance(patch, dict):
if not isinstance(target, dict):
target = {}
for name, value in patch.items():
if value is None and name in target:
del target[name]
else:
if name in target:
target[name] = merge_patch(target[name], value, arraykey)
else:
target[name] = value
return target
elif isinstance(patch, list):
if not isinstance(target, list):
target = []
dicted_target = {item[arraykey]:item for item in target}
dicted_patch = {item[arraykey]:item for item in patch}
for name, value in dicted_patch.items():
if value is None and name in dicted_target:
del dicted_target[name]
else:
if name in target:
dicted_target[name] = merge_patch(dicted_target[name], value, arraykey)
else:
dicted_target[name] = value
return list(dicted_target.values())
else:
return patch

print(merge_patch('x', 'y'))
print(merge_patch({'a':1},{'b':2}))
print(merge_patch([{'name':'a','x':'z'},{'name':'b'}],[{'name':'a','x':'y'},{'name':'c'}]))
我们有。请注意,目标将被函数改变(需要一些工作才能使其成为纯函数)。

关于json - 将嵌套字典合并到 d3 sunburst 的 Json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48280289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com