简体   繁体   English

如何递归嵌套python列表?

[英]How to nest python lists recursively?

list0 = [[]]
list0[0].append([])
list0[0][0].append([])
list0[0][0][0].append(["I'm in deep!"])
print(list0)

How do I achieve the nested effect above in a loop?如何在循环中实现上面的嵌套效果? Like how I can append lists to a list in a loop.就像我如何将列表附加到循环中的列表一样。

def list_deepener(layers):
    list1 = [[]]
    count = 0
    while count != layers:
        count += 1
        y = "list1" + ("[0]" * count)
        y.append([])
        print(x)
list_deepener(5)

I tried this but I couldn't think of any way to convert the string into code which would allow me to alter list1.我试过了,但我想不出任何方法可以将字符串转换为允许我更改 list1 的代码。

Suppose that we have mylist = [] to begin with, and we want to add lists to a certain depth.假设我们以mylist = []开头,并且我们想要将列表添加到某个深度。 The trick is to use a separate variable to keep track of where we are in the nesting process , and update that variable as we add each layer.诀窍是使用一个单独的变量来跟踪我们在嵌套过程中的位置,并在我们添加每一层时更新该变量。 Thus:因此:

>>> mylist = []
>>> current = mylist
>>> for i in range(5):
...     to_add = []
...     current.append(to_add)
...     current = to_add
... 
>>> mylist # 6 pairs of brackets: the initial list, plus 5 added nested lists
[[[[[[]]]]]]

There's many ways to solve this issue, but when you find yourself constructing code in strings and looking for ways to execute that, you're on the wrong path.有很多方法可以解决这个问题,但是当你发现自己在字符串中构建代码并寻找执行它的方法时,你就走错了路。

A recursive solution:递归解决方案:

def nest(x, levels):
    assert levels >= 0
    if levels == 0:
        return [x]
    else:
        return [nest(x, levels-1)]


print(nest("I'm in deep", 5))

An iterative solution:迭代解决方案:

def nest(x, levels):
    assert levels >= 0
    result = [x]
    for _ in range(levels):
        result = [result]
    return result


print(nest("I'm in deep", 5))

Another way to think about this is recursively.考虑这个问题的另一种方法是递归。 Consider what you want in the deepest part of your list (your 'base' case') and then layer on top for each iteration.考虑您在列表的最深处(您的“基本”案例)中想要什么,然后在每次迭代的顶部进行分层。

def deepen(layers: int) -> list:
    if layers <= 1:  # Base case, i.e. 'what happens at the bottom'
        return ["Deeper than deep"]
    else:  # Iterative case, i.e. 'what happens at each layer'
        return [deepen(layers - 1)]

This gives you the correct result:这会给你正确的结果:

>>> def deepen(layers: int) -> list:
...     if layers <= 1:
...         return ["Deeper than deep"]
...     else:
...         return [deepen(layers - 1)]
...
>>> deepen(5)
[[[[['Deeper than deep']]]]]

You could pass a range and initial string to functools.reduce for a simple one-liner:您可以将范围和初始字符串传递给functools.reduce以获得简单的单行:

from functools import reduce

layers = 4

reduce(lambda a, _: [a], range(layers), "I'm in deep!")
# [[[["I'm in deep!"]]]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM