Navigate manually with a cursor through nested lists by only providing quot;left()quot; and quot;right()quot; as commands?(仅通过提供“left()来使用光标手动导航嵌套列表.和“right()作为命令?)


尽管我用 python 编写,但我认为抽象概念对我和其他人来说更有趣.所以如果你喜欢,请伪代码:)

Eventhough I write in python I think the abstract concept is more interesting to me and others. So pseudocode please if you like :)


I have a list with items from one of my classes. Lets do it with strings and numbers here, it really doesn't matter. Its nested to any depth. (Its not really a list but a container class which is based on a list.)

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

请注意,两个 ['a', 'b', 'c'] 实际上是同一个容器.如果你改变一个,你就会改变另一个.可以编辑容器和项目,插入项目,最重要的容器可以多次使用.为避免冗余,不可能展平列表(我认为!),因为您失去了在一个容器中插入项目的能力,它会自动出现在所有其他容器中.

Note that both ['a', 'b', 'c'] are really the same container. If you change one you change the other. The containers and items can be edited, items inserted and most important containers can be used multiple times. To avoid redundancy its not possible to flatten the list (I think!) because you loose the ability to insert items in one container and it automatically appears in all other containers.

问题: 对于前端(只是带有 pythoncmd"模块的命令行),我想使用始终指向当前项目的光标导航此结构,以便可以读取或编辑.光标可以左右移动(用户的观点),并且应该表现得像列表不是嵌套列表而是平面列表.

The Problem: For the frontend (just commandline with the python "cmd" module) I want to navigate through this structure with a cursor which always points to the current item so it can be read or edited. The cursor can go left and right (users point of view) and should behave like the list is not a nested list but a flat one.


For a human this is super easy to do. You just pretend that in this list above the sublists don't exist and simply go from left to right and back.


For example if you are at the position of "3" in the list above and go right you get 'a' as next item, then 'b', 'c', and then "4" etc. Or if you go right from the "300" you get the "5" next.


And backwards: If you go left from "6" the next is 'c'. If you go left from "5" its "300".


So how do I do that in principle? I have one approach here but its wrong and the question is already so long that I fear most people will not read it :(. I can post it later.


P.S. Even if its hard to resist: The answer to this question is not "Why do you want to do this, why do you organize your data this way, why don't you [flatten the list| something out of my imagination] first? The problem is exactly what I've described here, nothing else. The data is structured by the nature of the problem this way.



One solution would be to store current index and/or depth information and use it to traverse the nested list. But that seems like a solution that would do a lot of complicated forking -- testing for ends of lists, and so on. Instead, I came up with a compromise. Instead of flattening the list of lists, I created a generator that creates a flat list of indices into the list of lists:

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


Then a simple function that extracts an innermost item from the list of lists based on an index tuple:

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


Now all you have to do is traverse the flat index list, in whatever way you 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]
>>> for i in indices:
...     print tuple_index(l, i),
1 2 3 a b c 4 d e 100 200 300 5 a b c 6


由于我在评论中发布在 ideone 上的基于堆栈的解决方案已接受此答案,并且由于最好不要使用外部粘贴箱作为答案代码,因此请注意 这个答案包含我的基于堆栈的解决方案.

Since this answer was accepted for the stack-based solution that I posted on ideone in the comments, and since it's preferable not to use external pastebins for answer code, please note that this answer also contains my stack-based solution.




python arbitrarily incrementing an iterator inside a loop(python在循环内任意递增迭代器)
Joining a set of ordered-integer yielding Python iterators(加入一组产生 Python 迭代器的有序整数)
Iterating over dictionary items(), values(), keys() in Python 3(在 Python 3 中迭代字典 items()、values()、keys())
What is the Perl version of a Python iterator?(Python 迭代器的 Perl 版本是什么?)
How to create a generator/iterator with the Python C API?(如何使用 Python C API 创建生成器/迭代器?)
Python generator behaviour(Python 生成器行为)