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.

Asked By: Kspr

||

Answers:

Sounds like an easy iterative problem:

  1. Initially, create an empty list lastitem
  2. Initially, create an empty list lastnumbering
  3. Take the next input element. Split it into a list along the "/" characters.
  4. Compare the list element-wise to the entries of lastitem.
    1. As long as the elements are equal, keep the corresponding elements in lastnumbering untouched.
    2. When change occurs, increase the corresponding value of lastnumbering and truncate it after.
    3. If you have reached the end of lastnumbering, but still have input segments, place the same number of 1 at the end of lastnumbering
    4. Save split input as lastitem
  5. Emit lastnumbering as dot-joined string
  6. 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.

Answered By: Marcus Müller
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.