So here's my problem:
I have successfully parsed a text file with line indention level in to a list like:
A = [[1,'a'],[1,'b'],[2,'c'],[2,'d'],[1,'e'],[2,'f']]
Each element in list A is a list of length 2. Each element corresponds to a line read from the text file. A[x][0] is the indent level of the line in the text file, A[x][1] is the content of the line where x is the index of any element in A.
For e.g. A[1] = [1,'b'] where 1 is the indent level and 'b' is the line text.
A[2] and A[3] are children of A[1] i.e. sub indented lines.
I am trying to get an output list which will be in the following format:
B = [['a'],['b',['c','d']],['e',['f']]]
This way when I iterate over B[x][0] I will get only the first level indented items and be able to recursively go to each element.
The algorithm should be able to handle infinite depth i.e if A[3] was followed by element [3,'z'] it should be a nested list of A[3].
I have explored some other posts that solve a similar problem and use itertools.groupby but unfortunately haven't been able to understand them enough to be able to apply it to my problem.
Really appreciate your help folks!
'b'in a sublist, although it's on the same level as'a'? Why isn't'd'in its own sublist then? Could you clarify the rules?['a', 'b', ['c', 'd'], 'e', ['f']]each sublist is "relevant" to the previous element.