在python中附加到递归列表

2024-10-03 02:48:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我所说的“递归”是这样的:[["hello", world, [1, 2, 3]], ["foo"]]。如何将4附加到[1, 2, 3]列表中

我有一门课:

class RabbitHole:
    def __init__(self, new_name, new_holes):
        self.name = new_name
        self.holes = new_holes

holesRabbitHole对象的列表。)

我想附加到具有“路径”(父孔的名称)的孔hat。我如何在保留整个“目录树”的同时更改/附加某些内容到帽子洞中(我在go中做过类似的事情,但在python中我不知道如何做)


Tags: 对象nameself路径hello列表newworld
1条回答
网友
1楼 · 发布于 2024-10-03 02:48:34

假设每个示例都有一个多维列表,则可以执行以下操作:

my_list = [['hello', 'world', [1, 2, 3]], ['foo']]

my_list[0][2].append(4)

print(my_list)

这将产生:

[['hello', 'world', [1, 2, 3, 4]], ['foo']]

相关问题 更多 >