我遇到了问题,但找不到解决方案。
我有一个这样的图表:
我有返回这样的图表的函数:
data = {
'Finition': {
'Metal': {
'colorCorrect1': {
'Color': {
'aiLayerShader2': {
'colorConstant1': {},
'colorConstant3': {},
'colorConstant2': {
'aiFloatToRgba1': {
'place2dTexture1': {}
}
},
'colorConstant4': {},
'colorConstant5': {
'aiFloatToRgba1': {
'place2dTexture1': {}
}
},
'colorConstant6': {}
}
}
}
}
}
}
我有一个主要组的列表(图中的 Blues Nodes):`
selection = ['Finition', 'Metal', 'Color', 'colorConstant2']
我需要一个可以返回特定组的节点列表(下一组之前)的函数:
返回值应该是这样的:
[
['Finition'],
['Metal', 'colorCorrect1'],
['Color', 'aiLayerShader2', 'colorConstant1', 'colorConstant3', 'colorConstant4', 'colorConstant5', 'colorConstant6', 'aiFloatToRgba1', 'place2dTexture1'],
['colorConstant2', 'aiFloatToRgba1', 'place2dTexture1']
]
我尝试了以下方法:
def search_recurssive(element=None, main={}, depth=0):
for key, value in main.items():
if key != element:
if isinstance(value, dict):
search_recurssive(element=element, main=value, depth=depth+1)
else:
pprint(value)
print depth
search_recurssive(element='Metal', main=data)
但是没有成功。接下来我可以尝试什么?
def search_recurssive(element=None, main={}, depth=0):
l = []
for key, value in main.items():
if key != element:
if isinstance(value, dict):
l.append(key)
l += search_recurssive(element=element, main=value, depth=depth+1)
else:
pprint(value)
print depth
return l
我刚刚调整了您的代码,使其按照您预期的方式工作
search_recurssive(element='Metal', main=data)
现在您需要调整它以获取下一个子图并搜索下一组。
编辑:只是调整了我之前的答案来对选择进行完整搜索。
selection = ['Finition', 'Metal', 'Color', 'colorConstant2']
next_group = data
def search_recurssive(element=None, main={}, depth=0):
global next_group
l = []
for key, value in main.items():
if key != element:
if isinstance(value, dict):
l.append(key)
l += search_recurssive(element=element, main=value, depth=depth+1)
else:
print(value)
print depth
next_group = main
return l
def search_selection(selection, data):
return [search_recurssive(element, next_group) for element in selection]
我是一名优秀的程序员,十分优秀!