gpt4 book ai didi

python - 如何在表示图形的字典上行走并返回元素列表

转载 作者:太空宇宙 更新时间:2023-11-03 19:55:58 24 4
gpt4 key购买 nike

我遇到了问题,但找不到解决方案。

我有一个这样的图表:

The node graph in Maya

我有返回这样的图表的函数:

    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]

关于python - 如何在表示图形的字典上行走并返回元素列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59529470/

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