如何在表示图形的字典上遍历并返回元素列表

2024-10-03 23:21:40 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,对不起,我的英语不是很好! 我有个问题,找不到解决办法。你知道吗

我有这样一个图表:

The node graph in Maya

我有一个函数,它返回如下图形:

    data = {
    'Finition': {
        'Metal': {
            'colorCorrect1': {
                'Color': {
                    'aiLayerShader2': {
                        'colorConstant1': {},
                        'colorConstant3': {},
                        'colorConstant2': {
                            'aiFloatToRgba1': {
                                'place2dTexture1': {}
                                }
                            },
                        'colorConstant4': {},
                        'colorConstant5': {
                            'aiFloatToRgba1': {
                                'place2dTexture1': {}
                                }
                            },
                        'colorConstant6': {}
                        }
                    }
                }
            }
        }
    }

我有一个主要组的列表(图片中的蓝色节点): `你知道吗

    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)

但没有起作用。非常感谢你的帮助!你知道吗


Tags: 函数searchdatavaluemainelementcolordepth
2条回答
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]

这里有一种效率很低的方法:

def getChildren(data,s):
    global selection
    res = [s]
    for child in data[s].keys():
        if child in selection:
            continue
        else:
            res.extend(getChildren(data[s], child))
    return res

def getDataPart(data,s):
    for key in data.keys():
        if key == s:
            return data
        res = getDataPart(data[key],s)
        if res is not None:
            return res

results = []

for s in selection:
    data_part = getDataPart(data,s)
    results.append(getChildren(data_part,s))

相关问题 更多 >