pythonbase = [
["1", "2", "3"],
["4", "", "5"],
["6", "7", "8"],
]
def f(x):
def concat(b):
if not x:
return b
if not b:
return f"[[{x}]]"
return f"{x}-{b}" if x else b
return apply_tree(base, concat)
def apply_tree(tree, func):
result = []
for e in tree:
if isinstance(e, str):
result.append(func(e))
else:
result.append(apply_tree(e, func))
return result
def print_tree_matrix(tree):
for layer in tree:
for row in zip(*layer):
print("\t".join(sum(row, [])))
print_tree_matrix(apply_tree(base, f))
1-1 | 1-2 | 1-3 | 2-1 | 2-2 | 2-3 | 3-1 | 3-2 | 3-3 |
1-4 | [[1]] | 1-5 | 2-4 | [[2]] | 2-5 | 3-4 | [[3]] | 3-5 |
1-6 | 1-7 | 1-8 | 2-6 | 2-7 | 2-8 | 3-6 | 3-7 | 3-8 |
4-1 | 4-2 | 4-3 | 1 | 2 | 3 | 5-1 | 5-2 | 5-3 |
4-4 | [[4]] | 4-5 | 4 | 5 | 5-4 | [[5]] | 5-5 | |
4-6 | 4-7 | 4-8 | 6 | 7 | 8 | 5-6 | 5-7 | 5-8 |
6-1 | 6-2 | 6-3 | 7-1 | 7-2 | 7-3 | 8-1 | 8-2 | 8-3 |
6-4 | [[6]] | 6-5 | 7-4 | [[7]] | 7-5 | 8-4 | [[8]] | 8-5 |
6-6 | 6-7 | 6-8 | 7-6 | 7-7 | 7-8 | 8-6 | 8-7 | 8-8 |