-1

I ended up into a problem.

Let's say i have a given array, or 4 separate lists( the columns )

P1 L1 V1 O1

P1 L1 V1 O2

P1 L1 V2 O1 

P1 L1 V3 O3

P2 L1 V2 O1
 
P2 L2 V1 O2

P2 L3 V4 O2

I would like to transform this into a python tree structure:

P1|

  |L1|

     |V1|

     |   |O1

     |   |O2

     |   |O3

     |V2|

     |  |O1

     |V3|

        |O3

P2|

  |L1|V2|O1

  |L2|V1|O2

  |L3|V4|O2

Ok now, this given array can change depending on the user input, it will always have this "kind of structure", but it's not defined a priori.

My goal is to define such a structure and have the possibility to know all the parents of a given children at the lowest level.

To sum up, as suggested from @trincot I enter an input/output data type:

Input: 4 lists, example:
['P1', 'P1', 'P1', 'P1', 'P1', 'P1', 'P1', 'P1', 'P1', 'P1', 'P1', 'P1', 'P2', 'P2', 'P2', 'P2', 'P2', 'P2', 'P2', 'P2', 'P2', 'P2']

output: One tree structure like this:
{'P1': {'L1': {'V1': {'O1': 'O1'}}, 'L2': {'V2': {'O2': 'O2'}, 'V1': {'O1': 'O1'}}}, 'P2': {'L1': {'V1': {'O1': 'O1'}}, 'L2': {'V2': {'O2': 'O2'}, 'V1': {'O1': 'O1'}}}}

In the output I would like then to know at last level what are the elements and know all the parents of this element.

Of course if another data type is more appropriate I would appreciate any suggestion.

Thanks for your help!

Andrea T
  • 15
  • 5
  • 1
    Please provide your input in the exact data structure you have for it, and the exact output structure (in Python syntax) you expect for it. – trincot Oct 14 '21 at 21:47
  • My input would be a a 4 lists containing the P, L, V, O. My output would be a tree structure like: P1{L1{V1{O1,O2,O3} , V2{O1, O2}}, {L2 {.....}}, ....} – Andrea T Oct 15 '21 at 08:26
  • 1
    Please edit your question and add this input/output in Python syntax, not (only) as an English description which can be ambiguous. – trincot Oct 15 '21 at 08:50
  • Is that example input related to the example input? Can you make them relate to eachother, so there is an actual test case to be made? – trincot Oct 15 '21 at 13:03

1 Answers1

1

I didn't see the connection between example input and example output, so I'm going to take a guess at what you want.

Here is an implementation:

# sample input
data = [
    ['P1', 'P1', 'P1', 'P1', 'P2', 'P2', 'P2'],
    ['L1', 'L1', 'L1', 'L1', 'L1', 'L2', 'L3'],
    ['V1', 'V1', 'V2', 'V3', 'V2', 'V1', 'V4'],
    ['O1', 'O2', 'O1', 'O3', 'O1', 'O2', 'O2']
]

forest = {}
for *path, last in zip(*data):
    node = forest
    for code in path:
        node = node.setdefault(code, {})
    node[last] = last

After running this code, forest will be the following nested dictionary:

{
  "P1": {
    "L1": {
      "V1": {
        "O1": "O1",
        "O2": "O2"
      },
      "V2": {
        "O1": "O1"
      },
      "V3": {
        "O3": "O3"
      }
    }
  },
  "P2": {
    "L1": {
      "V2": {
        "O1": "O1"
      }
    },
    "L2": {
      "V1": {
        "O2": "O2"
      }
    },
    "L3": {
      "V4": {
        "O2": "O2"
      }
    }
  }
}
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Hi,thanks for the help. yes this kind of I am looking for. Just 2 questions: 1) can you explain me the "node = tree" in your code where dowas tree come from? And how forest is related to the rest of the code? I do not see any call to this object – Andrea T Oct 18 '21 at 09:29
  • Ah that is a name change I did at the last moment and turns out I forgot one change. Now updated: there should be no `tree` variable. – trincot Oct 18 '21 at 10:09