python - List.append() 将所有元素更改为 append 项

标签 python list append maze

<分区>

我用 Python 编写的迷宫生成程序似乎有问题。我正在尝试随机创建一条在选定点分支的路径,这些点会随着路径的推移而存储。当迷宫到达死胡同时,它将通过测试最高值而不是弹出最高值并转到下一个值来对访问过的点进行排序,直到它到达一个不是死胡同的地方。但是,当我尝试将项目 append 到我用来保存我去过的空间的列表时,发生了一些奇怪的事情,我以前从未见过它。这是代码,查看它的最佳方式是多次运行它,直到它完全通过。我还没有真正找到解决死胡同问题的方法,所以如果有人也能帮我解决这个问题,那就太好了。

import random

width = 8

def check(x,y):
    """Figures out the directions that Gen can move while"""
    if x-1 == -1:
        maze[x][y][3] = 0 

    if x+1 == 8:
        maze[x][y][1] = 0

    if y+1 == 8:
        maze[x][y][2] = 0

    if y-1 == -1:
        maze[x][y][0] = 0

    if x + 1 in range(0,8) and visited[x+1][y] == False:
        maze[x][y][1] = 2

    if x - 1 in range(0,8) and visited[x-1][y] == False:
        maze[x][y][3] = 2

    if y + 1 in range(0,8) and visited[x][y+1] == False:
        maze[x][y][2] = 2

    if y - 1 in range(0,8) and visited[x][y-1] == False:
        maze[x][y][0] = 2



def Gen(x,y):
    visited[x][y] = True
    past.append(current)
    dirs = []
    check(x,y)
    print current

    if maze[x][y][0] == 2:
        dirs.append(0)
    if maze[x][y][1] == 2:
        dirs.append(1)
    if maze[x][y][2] == 2:
        dirs.append(2)
    if maze[x][y][3] == 2:
        dirs.append(3)

    pos = random.choice(dirs)

    print dirs

    maze[x][y][pos] = 1  

    if pos == 0:
        current[1] -= 1
    if pos == 1:
        current[0] += 1
    if pos == 2:
        current[1] += 1
    if pos == 3:
        current[0] -= 1

    if maze[x][y][0] == 4:
        maze[x][y][0] = 1

    if maze[x][y][1] == 4:
        maze[x][y][1] = 1

    if maze[x][y][2] == 4:
        maze[x][y][2] = 1

    if maze[x][y][3] == 4:
        maze[x][y][3] = 1

    print maze[x][y]
    print past, '\n'


#Build the initial values for the maze to be replaced later
maze = []
current = [0,0]
visited = []
past = []

#Generate empty 2d list with a value for each of the xy coordinates
for i in range(0,width):
    maze.append([])
    for q in range(0, width):
        maze[i].append([])
        for n in range(0, 4):
            maze[i][q].append(4)

#Makes a list of falses for all the non visited places
for x in range(0, width):
    visited.append([])
    for y in range(0, width):
        visited[x].append(False)

#Generates the walls
#for q in range(0, width):
#    for i in range(0, width):
#        check(q, i)

current = [0,0]

while current != [7,7]:
    Gen(current[0], current[1])
print maze

如您所见,它从 0,0 开始,然后找出可能的路径。它从中随机选择并将房间那一侧的值设置为 0,0 到 1,这意味着一条 channel 。 2 表示墙,0 表示越界。 4 只是一个占位符,因为所有值都应在迷宫完全生成时填满。

如果有人能帮助我,那将非常感谢。提前致谢。

最佳答案

我相信 current 列表只是多次复制到 past 中。所以您有多个相同列表的副本。

要修复:在 past.append(current) 行(def Gen(x,y): 下面两行),将其更改为 past .append(current[:]).

符号 list[:] 创建列表的副本。从技术上讲,您正在创建整个列表的一部分。

顺便说一下,更好的解决方案是不使用全局 current 变量 :)

关于python - List.append() 将所有元素更改为 append 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5280799/

相关文章:

java - 在 GUI 中逐项显示数组列表

python - 在可变长度元组列表中查找最常见的元素

python - Mac OS X 上的 Pip ImportError : cannot import name walk_packages

python - 为什么 list.remove() 在此 for 循环中不起作用?

python - 当元组不可变时,如何 append 到元组内的列表?

python - 将字典 append 到循环中的列表

python - 排序()返回无

python - For循环在while循环中中断

python - Python 中两个列表的异或

java - 为什么 List<List<Integer>> list = new ArrayList<ArrayList<Integer>>() ; 是错误的?