How to walk on a dictionary which represent a graph and return a list of element

AlexLaur

First of all, sory for my english which is not really nice! I have a problem and I can not find a solution.

I have a graph like this:

The node graph in Maya

I have function which return the graph like this:

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

I have a list of main groups (Blues Nodes in the picture): `

    selection = ['Finition', 'Metal', 'Color', 'colorConstant2']

I need a function that can return me the list of nodes (before the next group) for a specific group:

The return value should be like this:

    [
        ['Finition'],
        ['Metal', 'colorCorrect1'],
        ['Color', 'aiLayerShader2', 'colorConstant1', 'colorConstant3', 'colorConstant4', 'colorConstant5', 'colorConstant6', 'aiFloatToRgba1', 'place2dTexture1'],
        ['colorConstant2', 'aiFloatToRgba1', 'place2dTexture1']
    ]

I tried the following:

    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)

But it did not work. Thank you so much for your help !

Ardweaden

Here is a rather inefficient method:

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))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add a value to an element in a list which is dictionary?

How can I change the value of an element in a list which belongs to a dictionary?

Find an element from a list in a dictionary and return that key

How do I represent a graph given as an adjacency list in C#?

how to return a list of dictionary in graphene?

Return the element of a list which fulfills a certain condition

How to create a custom datatype which represent list of custom datatype in umbraco?

How to return keys which are not in a list?

How to preform operation which returns single for each element of list returned by observable and return it as a list?

How to walk through a dependency graph?

How to search for a specific string on a list from a dictionary and return all element that contain that string in mongodb?

Return the element of a list at which the function's return value is the highest

Return List which element belongs to up until the element position

How to sort a dictionary which is given in a list

Find which element of list is a key in a dictionary and what is it's value

How to represent a Set or Dictionary in Chapel?

Python string: how to query a dictionary inside a list, which are inside a dictionary

How do I represent dictionary of dictionary in Python

How to append the return of a function to a list and a dictionary in a for loop

How to return dictionary keys as a list in Python?

How to make the dictionary return a list value?

How to extract the dictionary if element in list matches

How to find is an element in dictionary is in list (Python)

How to append an element to list that is in dictionary in Python

How can I remove an dictionary element in list?

How do i add an Edge to a graph which is a List<list<int>>?

Common Lisp - Function which return an element appears n times in a list

how to return value inside a dictionary which is changed by a radio button

How to change an array element of a class module, which is item of a Dictionary?