我是否可以更改此代码以生成可以存储为二维列表的输出?
is there away for me to alter this code so that it produces an output that can be stored as a 2d list?
我可以用这段代码打印一个随机迷宫:
我想将其存储到二维列表中,以便我可以对其进行编辑。
我尝试过自行编辑,但此代码仅供打印,仅此而已。
def random_maze(w = 16, h = 8):
vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
ver = [["| "] * w + ['|'] for v in range(h)] + [[]]
hor = [["+--"] * w + ['+'] for v in range(h + 1)]
def go(x, y):
vis[y][x] = 1
d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
shuffle(d)
for (xx, yy) in d:
if vis[yy][xx]: continue
if xx == x: hor[max(y, yy)][x] = "+ "
if yy == y: ver[y][max(x, xx)] = " "
go(xx, yy)
go(randrange(w), randrange(h))
s = ""
for (a, b) in zip(hor, ver):
s += ''.join(a + ['\n'] + b + ['\n'])
return s
我希望代码输出像 [['+--', '+--'....等
以便我可以对其进行编辑。
这是我的解决方案。使用 np.append 函数将迷宫添加到二维数组中:
from random import randrange, shuffle
import numpy as np
w = 10
h = 10
vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
ver = [["| "] * w + ['|'] for v in range(h)] + [[]]
hor = [["+--"] * w + ['+'] for v in range(h + 1)]
def go(x, y):
vis[y][x] = 1
d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
shuffle(d)
for (xx, yy) in d:
if vis[yy][xx]: continue
if xx == x: hor[max(y, yy)][x] = "+ "
if yy == y: ver[y][max(x, xx)] = " "
go(xx, yy)
go(randrange(w), randrange(h))
s = ""
twoD_matrix = np.append([hor[0]], [ver[0]], axis=0)
for i in range(1, len(hor)):
twoD_matrix = np.append(twoD_matrix, [hor[i], ver[1]], axis = 0)
print(twoD_matrix)
或者如果您更喜欢列表的列表,您可以这样做:
from random import randrange, shuffle
import numpy as np
w = 10
h = 10
vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
ver = [["| "] * w + ['|'] for v in range(h)] + [[]]
hor = [["+--"] * w + ['+'] for v in range(h + 1)]
def go(x, y):
vis[y][x] = 1
d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
shuffle(d)
for (xx, yy) in d:
if vis[yy][xx]: continue
if xx == x: hor[max(y, yy)][x] = "+ "
if yy == y: ver[y][max(x, xx)] = " "
go(xx, yy)
go(randrange(w), randrange(h))
s = ""
twoD_list = []
for i in range(len(hor)):
twoD_list.append(hor[i])
twoD_list.append(ver[i])
print(twoD_list)
您所要做的只是在将 a
和 b
加入 s
的部分做一个小改动。您需要有一个额外的变量 matrix
来存储 2D 列表:
s = ""
matrix = []
for (a, b) in zip(hor, ver):
s += ''.join(a + ['\n'] + b + ['\n'])
matrix.append(a)
matrix.append(b)
return s, matrix
最后您可以像这样检索结果:
stringMatrix, matrix = random_maze()
我可以用这段代码打印一个随机迷宫:
我想将其存储到二维列表中,以便我可以对其进行编辑。
我尝试过自行编辑,但此代码仅供打印,仅此而已。
def random_maze(w = 16, h = 8):
vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
ver = [["| "] * w + ['|'] for v in range(h)] + [[]]
hor = [["+--"] * w + ['+'] for v in range(h + 1)]
def go(x, y):
vis[y][x] = 1
d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
shuffle(d)
for (xx, yy) in d:
if vis[yy][xx]: continue
if xx == x: hor[max(y, yy)][x] = "+ "
if yy == y: ver[y][max(x, xx)] = " "
go(xx, yy)
go(randrange(w), randrange(h))
s = ""
for (a, b) in zip(hor, ver):
s += ''.join(a + ['\n'] + b + ['\n'])
return s
我希望代码输出像 [['+--', '+--'....等 以便我可以对其进行编辑。
这是我的解决方案。使用 np.append 函数将迷宫添加到二维数组中:
from random import randrange, shuffle
import numpy as np
w = 10
h = 10
vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
ver = [["| "] * w + ['|'] for v in range(h)] + [[]]
hor = [["+--"] * w + ['+'] for v in range(h + 1)]
def go(x, y):
vis[y][x] = 1
d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
shuffle(d)
for (xx, yy) in d:
if vis[yy][xx]: continue
if xx == x: hor[max(y, yy)][x] = "+ "
if yy == y: ver[y][max(x, xx)] = " "
go(xx, yy)
go(randrange(w), randrange(h))
s = ""
twoD_matrix = np.append([hor[0]], [ver[0]], axis=0)
for i in range(1, len(hor)):
twoD_matrix = np.append(twoD_matrix, [hor[i], ver[1]], axis = 0)
print(twoD_matrix)
或者如果您更喜欢列表的列表,您可以这样做:
from random import randrange, shuffle
import numpy as np
w = 10
h = 10
vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
ver = [["| "] * w + ['|'] for v in range(h)] + [[]]
hor = [["+--"] * w + ['+'] for v in range(h + 1)]
def go(x, y):
vis[y][x] = 1
d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
shuffle(d)
for (xx, yy) in d:
if vis[yy][xx]: continue
if xx == x: hor[max(y, yy)][x] = "+ "
if yy == y: ver[y][max(x, xx)] = " "
go(xx, yy)
go(randrange(w), randrange(h))
s = ""
twoD_list = []
for i in range(len(hor)):
twoD_list.append(hor[i])
twoD_list.append(ver[i])
print(twoD_list)
您所要做的只是在将 a
和 b
加入 s
的部分做一个小改动。您需要有一个额外的变量 matrix
来存储 2D 列表:
s = ""
matrix = []
for (a, b) in zip(hor, ver):
s += ''.join(a + ['\n'] + b + ['\n'])
matrix.append(a)
matrix.append(b)
return s, matrix
最后您可以像这样检索结果:
stringMatrix, matrix = random_maze()