I am using Python 3.8.
The following list contains integers and I need to generate a hierarchy from this list:
list1 = [[15, 1], [22, 1], [23, 1], [121, 15], [101, 22], [105, 23], [106, 23], [108, 23], [155, 121], [120, 108], [19, 2], [25, 5], [33, 8], [35, 8], [28, 25], [29, 28]]
I need this result (output could be a list, e.g. [[[1, 15, 22, 23], [15, 121], [121, 155], [22, 101], [23, 105, 106, 108], [108, 120]], [2, 19], [[5, 25], [25, 28], [28, 29]], [8, 33, 35]]):
1 ---- 15 ---- 121 ---- 155
\---- 22 ---- 101
\--- 23 ---- 105
\----- 106
\---- 108 ---- 120
2 ---- 19
5 ---- 25 ---- 28 ----- 29
8 ---- 33
\---- 35
The hierarchy does not contain any duplicated items. Also first items of the lists in the list1 do not contain repeated/duplicated elements, but second items of the lists in the list1 contain.
How could this hierarchy be generated?
Note: I could do this by using some code but it could be very long and CPU cost could be high (actual list is very long).