python - Navigate manually with a cursor through nested lists by only providing "left()" and "right()" as commands? -


eventhough write in python think abstract concept more interesting me , others. pseudocode please if :)

i have list items 1 of classes. lets strings , numbers here, doesn't matter. nested depth. (its not list container class based on list.)

example: [1, 2, 3, ['a', 'b', 'c'] 4 ['d', 'e', [100, 200, 300]] 5, ['a', 'b', 'c'], 6]

note both ['a', 'b', 'c'] same container. if change 1 change other. containers , items can edited, items inserted , important containers can used multiple times. avoid redundancy not possible flatten list (i think!) because loose ability insert items in 1 container , automatically appears in other containers.

the problem: frontend (just commandline python "cmd" module) want navigate through structure cursor points current item can read or edited. cursor can go left , right (users point of view) , should behave list not nested list flat one.

for human super easy do. pretend in list above sublists don't exist , go left right , back.

for example if @ position of "3" in list above , go right 'a' next item, 'b', 'c', , "4" etc. or if go right "300" "5" next.

and backwards: if go left "6" next 'c'. if go left "5" "300".

so how do in principle? have 1 approach here wrong , question long fear people not read :(. can post later.

p.s. if hard resist: answer question not "why want this, why organize data way, why don't [flatten list| out of imagination] first? problem i've described here, nothing else. data structured nature of problem way.

one solution store current index and/or depth information , use traverse nested list. seems solution lot of complicated forking -- testing ends of lists, , on. instead, came compromise. instead of flattening list of lists, created generator creates flat list of indices list of lists:

def enumerate_nested(nested, indices):     i, item in enumerate(nested):         if isinstance(item, collections.iterable) , not isinstance(item, basestring):             new_indices in enumerate_nested(item, indices + (i,)):                 yield new_indices         else:             yield indices + (i,) 

then simple function extracts innermost item list of lists based on index tuple:

def tuple_index(nested_list, index_tuple):     in index_tuple:         nested_list = nested_list[i]     return nested_list 

now have traverse flat index list, in whatever way like.

>>> indices = list(enumerate_nested(l, tuple())) >>> print l [1, 2, 3, ['a', 'b', 'c'], 4, ['d', 'e', [100, 200, 300]], 5, ['a', 'b', 'c'], 6] >>> in indices: ...     print tuple_index(l, i), ...  1 2 3 b c 4 d e 100 200 300 5 b c 6 

since answer accepted stack-based solution posted on ideone in comments, , since it's preferable not use external pastebins answer code, please note this answer contains stack-based solution.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -