In Python, iterate over list representing a flat nested structure and create a new list with the index of each element
Question:
I have a list with generic words of generic length, representing a flat representation of a nested structure where each level is separated by "/".
['apple', 'apple/dog', 'apple/dog/banana', 'apple/dog/apple', 'apple/cat', 'cold', 'cold/banana', ....]
I want to, with respect to list input order, create a string representation of the index of the flat representatin of the nested structure.
['1' , '1.1', '1.1.1', '1.1.2', '1.2', '2', '2.1',....]
Does anyone have any tips on how to do it or how to approach the problem? Would greatly appreciate it.
Answers:
Sounds like an easy iterative problem:
- Initially, create an empty list lastitem
- Initially, create an empty list lastnumbering
- Take the next input element. Split it into a list along the "/" characters.
- Compare the list element-wise to the entries of lastitem.
- As long as the elements are equal, keep the corresponding elements in lastnumbering untouched.
- When change occurs, increase the corresponding value of lastnumbering and truncate it after.
- If you have reached the end of lastnumbering, but still have input segments, place the same number of 1 at the end of lastnumbering
- Save split input as lastitem
- Emit lastnumbering as dot-joined string
- Move on to 3.
This is but a Lexer. It looks at the context of each input it sees, tokenizes it and emits a tokenized linear representation of the input without any restructuring.
I have a list with generic words of generic length, representing a flat representation of a nested structure where each level is separated by "/".
['apple', 'apple/dog', 'apple/dog/banana', 'apple/dog/apple', 'apple/cat', 'cold', 'cold/banana', ....]
I want to, with respect to list input order, create a string representation of the index of the flat representatin of the nested structure.
['1' , '1.1', '1.1.1', '1.1.2', '1.2', '2', '2.1',....]
Does anyone have any tips on how to do it or how to approach the problem? Would greatly appreciate it.
Sounds like an easy iterative problem:
- Initially, create an empty list lastitem
- Initially, create an empty list lastnumbering
- Take the next input element. Split it into a list along the "/" characters.
- Compare the list element-wise to the entries of lastitem.
- As long as the elements are equal, keep the corresponding elements in lastnumbering untouched.
- When change occurs, increase the corresponding value of lastnumbering and truncate it after.
- If you have reached the end of lastnumbering, but still have input segments, place the same number of 1 at the end of lastnumbering
- Save split input as lastitem
- Emit lastnumbering as dot-joined string
- Move on to 3.
This is but a Lexer. It looks at the context of each input it sees, tokenizes it and emits a tokenized linear representation of the input without any restructuring.