【发布时间】:2020-11-30 05:54:57
【问题描述】:
我今天正在为一门课程做作业,任务是创建一个井字游戏板。可能性方法将井字棋作为输入,并检查是否有任何值是“0”,这意味着它是一个开放空间。我的计划是将 0 的位置添加到一个称为位置的数组中,然后在函数末尾返回位置。但是,当我尝试将 0 的位置附加到位置数组时,我不断遇到这个问题:“连接轴的所有输入数组维度必须完全匹配,但是沿着维度 0,索引 0 处的数组大小为 2并且索引 1 处的数组大小为 1"。有谁知道如何解决这个问题?谢谢
import numpy as np
def create_board():
board = np.zeros((3,3), dtype = "int")
return board
def place(board, player, position):
x, y = position
board[x][y] = player
def posibilities(board):
locations = np.empty(shape=[2,0])
for i in range(len(board)):
for x in range(len(board[0])):
if board[i][x] == 0:
locations = np.append(locations, [[i,x]], axis=1)
print(locations)
posibilities(create_board())
【问题讨论】:
-
使用列表和列表追加。
np.append速度慢且难以正确使用。
标签: python arrays numpy append