【问题标题】:Appending to a recursive list in python [closed]在python中附加到递归列表[关闭]
【发布时间】:2020-02-11 04:48:22
【问题描述】:

我所说的“递归”是这样的:[["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 对象的列表。)

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

【问题讨论】:

标签: python list recursion


【解决方案1】:

假设您的示例有一个多维列表,您可以这样做:

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

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

print(my_list)

产量:

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

【讨论】:

  • 阅读完整的问题。我需要追加到 RabbitHole 类,只知道我描述的“路径”
猜你喜欢
  • 2015-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 2021-11-09
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
相关资源
最近更新 更多