Python 从函数返回时覆盖数组
Python overwriting array when returned from function
对于人工智能评估,我试图通过将一个数组作为 return 值发送到生成函数中来获得 4 个单独的数组。发送数组 1 时它正常工作。但是当发送数组 2、3 和 4 时,它将覆盖之前生成的数组。最后一个数组array4此时的输出为:
['#', '#', '#', '#', '#']
['#', 'G', '☐', 'G', '#']
['#', '☐', 'P', '•', '#']
['#', 'P', '•', 'P', '#']
['#', '#', '#', '#', '#']
array4 的理想输出是:
['#', '#', '#', '#', '#']
['#', 'G', '•', 'G', '#']
['#', '☐', '☐', '•', '#']
['#', 'P', '•', '•', '#']
['#', '#', '#', '#', '#']
下面是我的完整 Python 代码:
def solver():
matrix = [['#', '#', '#', '#', '#'], ['#', 'G', '•', 'G', '#'], ['#', '☐', '☐', '•', '#', ],
['#', '•', 'P', '•', '#'], ['#', '#', '#', '#', '#']]
countx = 0
county = 0
cordp = []
for x in matrix:
county += 1
for y in x:
countx += 1
if y == 'P':
cordp = [countx, county]
countx = 0
print(x)
# nieuwe stap
# wat is huidige positie
cordp[0], cordp[1] = cordp[1]-1, cordp[0]-1
n = gen_matrix(matrix, cordp, 0,-1)
e = gen_matrix(matrix, cordp, 1,0)
s = gen_matrix(matrix, cordp, 0,1)
w = gen_matrix(matrix, cordp, -1,0)
for x in n:
print(x)
def gen_matrix(matrixnew, cordp, x, y):
print (x)
print(y)
if matrixnew[cordp[0]+y][cordp[1]+x] == '☐':
if matrixnew[cordp[0]+y*2][cordp[1]+x*2] == '#':
print("ik kan doos niet verplaatsen")
else:
print("IK HEB EEN BOX en kan deze verplaatsen")
matrixnew[cordp[0]+y*2][cordp[1]+x*2] = '☐'
matrixnew[cordp[0]+y][cordp[1]+x] = 'P'
matrixnew[cordp[0]][cordp[1]] = '•'
elif matrixnew[cordp[0]+y][cordp[1]+x] == '•':
print("ik heb een bolletje")
matrixnew[cordp[0]+y][cordp[1]+x] = 'P'
matrixnew[cordp[0]][cordp[1]] = '•'
elif matrixnew[cordp[0]+y][cordp[1]+x] == '#':
print("ik heb een muur")
return matrixnew
solver()
正如 Paul 在评论中指出的那样,您正在覆盖矩阵,因为 python 通过引用而不是值传递列表。
修复方法是在修改矩阵之前复制它们。
我们可以复制一个二维矩阵,即列表的列表,像这样:
matrix_copy = [list(row) for row in original_matrix]
所以我们可以替换这个
n = gen_matrix(matrix, cordp, 0,-1)
e = gen_matrix(matrix, cordp, 1,0)
s = gen_matrix(matrix, cordp, 0,1)
w = gen_matrix(matrix, cordp, -1,0)
有了这个
n = gen_matrix([list(row) for row in matrix], cordp, 0,-1)
e = gen_matrix([list(row) for row in matrix], cordp, 1,0)
s = gen_matrix([list(row) for row in matrix], cordp, 0,1)
w = gen_matrix([list(row) for row in matrix], cordp, -1,0)
为每个运行提供gen_matrix
矩阵的新副本。
对于人工智能评估,我试图通过将一个数组作为 return 值发送到生成函数中来获得 4 个单独的数组。发送数组 1 时它正常工作。但是当发送数组 2、3 和 4 时,它将覆盖之前生成的数组。最后一个数组array4此时的输出为:
['#', '#', '#', '#', '#']
['#', 'G', '☐', 'G', '#']
['#', '☐', 'P', '•', '#']
['#', 'P', '•', 'P', '#']
['#', '#', '#', '#', '#']
array4 的理想输出是:
['#', '#', '#', '#', '#']
['#', 'G', '•', 'G', '#']
['#', '☐', '☐', '•', '#']
['#', 'P', '•', '•', '#']
['#', '#', '#', '#', '#']
下面是我的完整 Python 代码:
def solver():
matrix = [['#', '#', '#', '#', '#'], ['#', 'G', '•', 'G', '#'], ['#', '☐', '☐', '•', '#', ],
['#', '•', 'P', '•', '#'], ['#', '#', '#', '#', '#']]
countx = 0
county = 0
cordp = []
for x in matrix:
county += 1
for y in x:
countx += 1
if y == 'P':
cordp = [countx, county]
countx = 0
print(x)
# nieuwe stap
# wat is huidige positie
cordp[0], cordp[1] = cordp[1]-1, cordp[0]-1
n = gen_matrix(matrix, cordp, 0,-1)
e = gen_matrix(matrix, cordp, 1,0)
s = gen_matrix(matrix, cordp, 0,1)
w = gen_matrix(matrix, cordp, -1,0)
for x in n:
print(x)
def gen_matrix(matrixnew, cordp, x, y):
print (x)
print(y)
if matrixnew[cordp[0]+y][cordp[1]+x] == '☐':
if matrixnew[cordp[0]+y*2][cordp[1]+x*2] == '#':
print("ik kan doos niet verplaatsen")
else:
print("IK HEB EEN BOX en kan deze verplaatsen")
matrixnew[cordp[0]+y*2][cordp[1]+x*2] = '☐'
matrixnew[cordp[0]+y][cordp[1]+x] = 'P'
matrixnew[cordp[0]][cordp[1]] = '•'
elif matrixnew[cordp[0]+y][cordp[1]+x] == '•':
print("ik heb een bolletje")
matrixnew[cordp[0]+y][cordp[1]+x] = 'P'
matrixnew[cordp[0]][cordp[1]] = '•'
elif matrixnew[cordp[0]+y][cordp[1]+x] == '#':
print("ik heb een muur")
return matrixnew
solver()
正如 Paul 在评论中指出的那样,您正在覆盖矩阵,因为 python 通过引用而不是值传递列表。
修复方法是在修改矩阵之前复制它们。
我们可以复制一个二维矩阵,即列表的列表,像这样:
matrix_copy = [list(row) for row in original_matrix]
所以我们可以替换这个
n = gen_matrix(matrix, cordp, 0,-1)
e = gen_matrix(matrix, cordp, 1,0)
s = gen_matrix(matrix, cordp, 0,1)
w = gen_matrix(matrix, cordp, -1,0)
有了这个
n = gen_matrix([list(row) for row in matrix], cordp, 0,-1)
e = gen_matrix([list(row) for row in matrix], cordp, 1,0)
s = gen_matrix([list(row) for row in matrix], cordp, 0,1)
w = gen_matrix([list(row) for row in matrix], cordp, -1,0)
为每个运行提供gen_matrix
矩阵的新副本。