简体   繁体   中英

Python: create tree structure from given array/list

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!

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 = tree
    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"
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM