I have a matrix of elements in python structured like this:
matrix["S"]=["H","S","SC"]
In my logic the list attached to the matrix key must become a nested json object in which each element of the list contains the following one, and the key must be the outermost object e.g.
"S":{
"H":{
"contains":[{
"S":{
"contains":[
"SC":{
"contains":[
None
]
}
}]
}
}]
}
}
My solution is correct and it's something like this:
for key in matrix:
temp = None
for item in reversed(matrix[key]):
node = {"embedded": item[1]["embedded"], "macroType": item[1]["macroType"], "contains": [temp], "ignore":item[1]["ignore"]}
temp = {list(item[0].labels)[0]:node}
response["instances"].append({key:temp})
response["instance"] contains all the objects.
My problem is when "special" cases happen e.g. "V" cannot be contained inside "SC"
matrix["V"]=["H","S","SC","V","VC"] must output
"V":{
"H":{
"contains":[{
"S":{
"contains":[{
"SC":{
"contains":[
None
]
},{
"V":{
"contains":[{
"VC":{
"contains":[
None
]
}
}]
}
}]
}
}]
}
}
because in my logic, S contains V at the same level of SC and there's a rule that states that SC cannot contain V. How can I pack "V" and "VC" until I can insert inside something but still packing and nesting the following objects?
EDIT my code just nests all the object from the bottom to the top of the list.
"V":{
"H":{
"contains":[{
"S":{
"contains":[{
"SC":{
"contains":[{
"V":{
"contains":[{
"VC":{
"contains":[
None
]
}
}]
}
}]
}
}]
}
}]
}
}
I really don't know how to implement what I need