在python中追加到递归列表[关闭]

问题描述 投票:-1回答:1

我所说的“递归”是这样的:[["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的孔clothes/head/hat。我如何在保留整个“目录树”的同时对帽子孔进行更改/附加(我已经在go中完成了类似的操作,但是我不知道如何在python中进行操作。)

python list recursion
1个回答
2
投票

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

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

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

print(my_list)

哪个产量:

[['hello', 'world', [1, 2, 3, 4]], ['foo']]
© www.soinside.com 2019 - 2024. All rights reserved.