imolinuevo
imolinuevo

Reputation: 556

Python cartesian product in nested dict

I know that in order to get the combination of an list of list the cartesian product must be used, but in my case I need the result to be a dictionary where combination subgroups are nested inside so I can use the result as a JSON for a recursive angular directive that shows elements nested in N depth levels.

Here is a data input example:

[
    [{"cluster_id":1,"id":1,"name":"A"}, {"cluster_id":1,"id":2,"name":"B"}, {"cluster_id":1,"id":3,"name":"C"}],
    [{"cluster_id":2,"id":1,"name":"D"}, {"cluster_id":2,"id":2,"name":"E"}, {"cluster_id":2,"id":3,"name":"F"}],
    [{"cluster_id":3,"id":1,"name":"G"}, {"cluster_id":3,"id":2,"name":"H"}, {"cluster_id":3,"id":3,"name":"I"}]
]

That should result like this:

[{"cluster_id":1,"id":1,"name":"A", "subcluster": [
        {"cluster_id":2,"id":1,"name":"D", "subcluster": [
                {"cluster_id":3,"id":1,"name":"G"},
                {"cluster_id":3,"id":2,"name":"H"},
                {"cluster_id":3,"id":3,"name":"I"},
            ]
        },
        {"cluster_id":2,"id":2,"name":"E", "subcluster": [
                {"cluster_id":3,"id":1,"name":"G"},
                {"cluster_id":3,"id":2,"name":"H"},
                {"cluster_id":3,"id":3,"name":"I"},
            ]
        },
        {"cluster_id":2,"id":3,"name":"F", "subcluster": [
                {"cluster_id":3,"id":1,"name":"G"},
                {"cluster_id":3,"id":2,"name":"H"},
                {"cluster_id":3,"id":3,"name":"I"},
            ]
        }
    ]
},
{"cluster_id":1,"id":2,"name":"B", "subcluster": [
        {"cluster_id":2,"id":1,"name":"D", "subcluster": [
                {"cluster_id":3,"id":1,"name":"G"},
                {"cluster_id":3,"id":2,"name":"H"},
                {"cluster_id":3,"id":3,"name":"I"},
            ]
        },
        {"cluster_id":2,"id":2,"name":"E", "subcluster": [
                {"cluster_id":3,"id":1,"name":"G"},
                {"cluster_id":3,"id":2,"name":"H"},
                {"cluster_id":3,"id":3,"name":"I"},
            ]
        },
        {"cluster_id":2,"id":3,"name":"F", "subcluster": [
                {"cluster_id":3,"id":1,"name":"G"},
                {"cluster_id":3,"id":2,"name":"H"},
                {"cluster_id":3,"id":3,"name":"I"},
            ]
        }
    ]
},
{"cluster_id":1,"id":3,"name":"C", "subcluster": [
        {"cluster_id":2,"id":1,"name":"D", "subcluster": [
                {"cluster_id":3,"id":1,"name":"G"},
                {"cluster_id":3,"id":2,"name":"H"},
                {"cluster_id":3,"id":3,"name":"I"},
            ]
        },
        {"cluster_id":2,"id":2,"name":"E", "subcluster": [
                {"cluster_id":3,"id":1,"name":"G"},
                {"cluster_id":3,"id":2,"name":"H"},
                {"cluster_id":3,"id":3,"name":"I"},
            ]
        },
        {"cluster_id":2,"id":3,"name":"F", "subcluster": [
                {"cluster_id":3,"id":1,"name":"G"},
                {"cluster_id":3,"id":2,"name":"H"},
                {"cluster_id":3,"id":3,"name":"I"},
            ]
        }
    ]
}]

I would like to know how to generate this output using Pyhton if possible, but any help is welcome.

Upvotes: 0

Views: 277

Answers (1)

imolinuevo
imolinuevo

Reputation: 556

I was finally able to solve it with the following recursive function:

def generate_tree(filters, depth):
    if depth == len(filters)-1:
        return filters[depth]
    else:
        return [{'name': i['name'], 'id': i['id'], 'cycle_cluster_id': i['cycle_cluster_id'], 'subcluster': generate_tree(filters, depth+1)} for i in filters[depth]]

Upvotes: 1

Related Questions