可以有两种表达方式:
嵌套循环:如何在Python中构建动态嵌套循环, 其中前一个外部迭代的输出 len(list) Loop 是内循环应该迭代的次数?
Trees:如何在Python中构建动态树,其中 父级的输出 len(list) 是该父级的子级数 有..等等?
我有 n 个级别(n 是已知的并且是用户输入的)。将其可视化为节点会更容易,所以假设某个级别 i 中的每个节点输出一个列表。该列表的长度是该节点在 i+1 层的子节点数。节点和级别的数量是动态的。级别数是用户给定的参数,但每个级别的节点数是非常动态的,并且取决于父节点(在上一级别中)的输出列表长度。
基本上,它们是嵌套循环,其中每个内部循环的迭代次数取决于其外部循环的输出列表的长度。
我在下面附上了一张图片,以及关卡的结构是怎样的。
如何用Python编写这个代码?我已经尝试使用递归编写代码好几天了,但我似乎无法正确执行。
感谢您提前提供的帮助。
这是我的尝试:
def iterate(l: list, levels: int, current_level: int):
if levels == current_level:
return
for i in range(len(l)):
print(f"Level:{current_level}, iteration: {i}")
# do something to get new list .. eg:
new_list = list(range(1, random.randint(1, 10)+1))
print("-----------------")
return iterate(new_list, levels, current_level + 1)
迭代([7,4,2,4],3,1)
这是输出:
Level:1, iteration: 0
Level:1, iteration: 1
Level:1, iteration: 2
Level:1, iteration: 3
-----------------
Level:2, iteration: 0
Level:2, iteration: 1
Level:2, iteration: 2
Level:2, iteration: 3
Level:2, iteration: 4
Level:2, iteration: 5
但是,我想要的输出是这样的:
Level:1, iteration: 0
Level:2, iteration: 0
Level:2, iteration: 1
Level:2, iteration: 2
Level:1, iteration: 1
Level:2, iteration: 0
Level:2, iteration: 1
Level:2, iteration: 2
Level:2, iteration: 3
Level:2, iteration: 4
Level:1, iteration: 2
Level:2, iteration: 0
Level:2, iteration: 1
Level:1, iteration: 3
Level:2, iteration: 0
Level:2, iteration: 1
Level:2, iteration: 2
Level:2, iteration: 3
Level:2, iteration: 4
Level:2, iteration: 5
all_data = []
num_levels = 4 (given by user)
LEVEL: 1, and loop number: 0, variables: [4] - - output len: 5 #the num iter for next levels
LEVEL: 2, and loop number: 0, variables: [5] - - output len: 4 #the num iter for next levels
LEVEL: 3, and loop number: 0, variables: [4] - - output len: 2 #the num iter for next levels
LEVEL: 4, and loop number: 0, variables: [2] # reached end - level 4. . . append base data here and of the prev levels to all_data
LEVEL: 4, and loop number: 1, variables: [2] # reached end - level 4. . . append base data here and of the prev levels to all_data
LEVEL: 3, and loop number: 1, variables: [4]
. . .
LEVEL: 3, and loop number: 2, variables: [4]
. . .
LEVEL: 3, and loop number: 3, variables: [4]
. . .
LEVEL: 2, and loop number: 1, variables: [5]
. . .
. . .
LEVEL: 2, and loop number: 2, variables: [5]
. . .
. . .
LEVEL: 2, and loop number: 3, variables: [5]
. . .
. . .
LEVEL: 2, and loop number: 4, variables: [5]
. . .
. . .
LEVEL: 1, and loop number: 1, variables: [4] - - output len: 2 #the num iter for next levels
LEVEL: 2, and loop number: 0, variables: [2]
. . .
. . .
LEVEL: 2, and loop number: 1, variables: [2]
. . .
. . .
LEVEL: 1, and loop number: 2, variables: [4] - - output len: 3 #the num iter for next levels
LEVEL: 2, and loop number: 0, variables: [3]
. . .
. . .
LEVEL: 2, and loop number: 1, variables: [3]
. . .
. . .
LEVEL: 2, and loop number: 2, variables: [3]
. . .
. . .
LEVEL: 1, and loop number: 3, variables: [4] - - output len: 5 #the num iter for next levels
LEVEL: 2, and loop number: 0, variables: [5]
. . .
. . .
LEVEL: 2, and loop number: 1, variables: [5]
. . .
. . .
LEVEL: 2, and loop number: 2, variables: [5]
. . .
. . .
LEVEL: 2, and loop number: 3, variables: [5]
. . .
. . .
LEVEL: 2, and loop number: 4, variables: [5]
. . .
. . .
不确定我是否理解你的问题,但这是一个潜在的解决方案。使用
pip install asciitree
安装 aciitree 库
from asciitree import LeftAligned
def build_tree(level, max_level):
if level == max_level+1:
return {}
else:
return {i: build_tree(level+1, max_level) for i in range(1, level+1)}
tree = build_tree(1, 4)
tr = LeftAligned()
print(tr(tree))