将任意长度的位置列表 [4, 1, 2] 转换为嵌套列表的索引
假设这个列表
nestedList = ["a", "b", [1, 2, 3], "c",[4, 5, 6, [100, 200, 300]], "d"]
我有一个函数,它返回任意深度的嵌套列表的位置列表。 示例:
[2, 1] -> "2"
[5] -> "d"
[4, 3, 2] -> "300"
正如您所看到的,一开始并不清楚有多少层嵌套。
其他问题 对于列表修改,我想使用 [:] 或 [4:] 或 [0:1] 符号。
对于人类来说,这很容易做到:只需添加所需数量的索引位置即可。
nestedList[2][1]
nestedList[5]
nestedList[4][3][2]
nestedList[4][1:] = NewItem + nestedList[4][1:] #insert item
nestedList[2][1] = [] #remove item
然而,这种方法不会产生任何结果,因为我必须将字符串附加在一起并稍后对其进行评估。明显的废话:)
处理具有未知数量索引位置的嵌套列表并且仍然具有像普通列表一样处理它的功能(读取、修改、插入、删除)的最佳方法是什么
我希望有一个答案。
PS 列表必须保持嵌套。扁平化不是一种选择。
Assuming this list
nestedList = ["a", "b", [1, 2, 3], "c",[4, 5, 6, [100, 200, 300]], "d"]
I have a function that returns a position list for a nested list of arbitrary depth.
Examples:
[2, 1] -> "2"
[5] -> "d"
[4, 3, 2] -> "300"
As you can see it is not clear in the beginning how many levels of nesting there are.
Additional Problem
For list modifications I want to use the [:] or [4:] or [0:1] notations.
For a human its very easy to do: simply add as many index position as you need to.
nestedList[2][1]
nestedList[5]
nestedList[4][3][2]
nestedList[4][1:] = NewItem + nestedList[4][1:] #insert item
nestedList[2][1] = [] #remove item
However this approach does not lead anywhere since I had to append strings together and eval them later. Obvious nonsense :)
What is the best way to handle a nested list with an unknown number of index positions and still have the functionality to handle it like a normal list (read, modify, insert, remove)
I hope there is an answer to that.
P.S. the list must remain nested. Flattening is not an option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一部分很简单。
第二部分需要更多的努力,但仍然是可行的。暗示:
The first part is easy enough.
The second part requires much more effort, but is still doable. Hint:
我终于有时间摆弄这个了。我有点得意忘形了。虽然很长,不过我还是贴一下吧。我添加了
set_item
、insert
、delete
、find
和find_left
方法,如下以及一些允许破坏游标抽象的低级操作的私有方法。我还添加了一个move_cursor
方法,对于超出范围或指向非顶级对象的索引元组抛出IndexError
。基本上,(应该)保证只要你只使用公共函数,光标总是指向顶级对象,并且插入和删除都发生在顶级。从这里,您应该能够安全地实现
__getitem__
、__setitem__
、__delitem__
等,甚至可能__getslice__
,__setslice__
。然而,还有一些问题。光标始终指向顶级对象的限制使得迭代嵌套列表变得非常容易,就好像它是一个平面列表一样。但这也意味着光标无法指向较低级别的对象,因此单独使用
insert
无法进行某些类型的插入。例如,假设您有三个列表:现在将此嵌套结构放入 NLI 中,移至
5
,然后尝试插入。正如您所看到的,您可以将某些内容插入到
l2
中,但无法轻松地将某些内容插入到l3
中。事实上,现在要做到这一点,您必须使用私有函数,这会以一种令人不快的方式破坏游标抽象:肯定有方法可以实现安全的公共
insert_ Between_branches
方法,但它们涉及更多比我现在关心的复杂性。当尝试在
4
之后插入值时,会出现另一个问题。如您所见,您可以在5
之前将值插入到l2
中,但如果将光标移动到4
并insert
,您很快就会意识到您无法在l1
内的4
之后插入内容。从平面访问的角度来看,在4之后插入和在5之前插入是同一件事,但是从嵌套列表的角度来看,它们是不同的。由于
insert
实际上是left_insert
,因此可以使用right_insert
方法部分纠正此问题(反过来,该方法将无法插入到l1) 的开头。这些问题可能可以通过允许光标指向较低级别的对象来更普遍地处理,但这将使平面访问更加复杂。简而言之,任何纠正这些问题的尝试都会导致更大的复杂性,无论是在界面的平面还是嵌套方面。
(这实际上就是为什么我仍然更喜欢简单的
enumerate_nested
方法!一个正确的树结构,所有节点都有值(而不仅仅是顶级节点)也可能更简单、更好,但是编码仍然很有趣。)I finally had some time to fiddle around with this. I got a little carried away. It's long, but I'm pasting it anyway. I added
set_item
,insert
,delete
,find
, andfind_left
methods, as well as some private methods to allow low-level manipulation that breaks the cursor abstraction. I also added amove_cursor
method that throws anIndexError
for index tuples that are out of range or point to a non-toplevel object.Basically, it (should) be guaranteed that as long as you use public functions only, the cursor always points to a top-level object, and insertions and deletions all happen at the top level. From here, you should be able to safely implement
__getitem__
,__setitem__
,__delitem__
, etc, and maybe even__getslice__
,__setslice__
.However, there are a couple of wrinkles. The restriction that the cursor always points to a toplevel object makes it very easy to iterate over the nested list as though it were a flat list. But it also means that the cursor can't point at lower-level objects, and hence some kinds of insertions can't happen using
insert
alone. For example, say you have three lists:Now put this nested structure in a NLI, move to
5
, and try to insert.As you can see, you can insert something into
l2
, but you can't easily insert something intol3
. In fact, to do so right now, you have to use a private function, which breaks the cursor abstraction in an unpleasant way:There are surely ways to implement a safe public
insert_between_branches
method, but they involve more complication than I care to bother with right now.Another problem appears when one tries to insert a value after
4
. As you've seen, you can insert a value intol2
before5
, but if you move the cursor to4
andinsert
, you'll quickly realize that you can't insert something after4
insidel1
.From the perspective of flat access, inserting after 4 and inserting before 5 are the same thing, but from the perspective of the nested list, they are different. Since
insert
is effectively aleft_insert
, this problem could be partially rectified with aright_insert
method (which would, in turn, be unable to insert at the beginning of l1).These problems could probably be dealt with more generally by allowing the cursor to point to lower-level objects, but that would make flat access more complicated. In short, any attempt to rectify these problems will lead to greater complexity, either on the flat or the nested side of the interface.
(That is actually why I still prefer the simple
enumerate_nested
method! A proper tree structure with values at all nodes (and not just top-level nodes) might also be simpler and better. But this was fun to code nonetheless.)