【问题标题】:Having trouble with numpy append functionnumpy追加功能有问题
【发布时间】: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


【解决方案1】:

正如@hpaulj 建议改用 list 并在最后将其更改为 np.array ,即:

def posibilities(board):
    locations = []
    for i in range(len(board)):
        for x in range(len(board[0])):
            if board[i][x] == 0:
                locations.append([[i,x]])
    locations = np.array(locations) # or np.concatenate(locations) depending what you want
    print(locations)

这是执行此操作的正确方法,因为 python 列表是可变的,而 numpy 数组不是。

【讨论】:

  • @TejaKoduru 我在 np.array(locations) 之外添加了 np.concatenate(locations) 因为你可能想要这个
【解决方案2】:
In [530]: board = np.random.randint(0,2,(3,3))                                                       
In [531]: board                                                                                      
Out[531]: 
array([[0, 0, 0],
       [1, 0, 1],
       [0, 1, 0]])

看起来您正在尝试收集板上为 0 的位置。argwhere 做得很好:

In [532]: np.argwhere(board==0)                                                                      
Out[532]: 
array([[0, 0],
       [0, 1],
       [0, 2],
       [1, 1],
       [2, 0],
       [2, 2]])

附上列表:

In [533]: alist = []                                                                                 
In [534]: for i in range(3): 
     ...:     for j in range(3): 
     ...:         if board[i,j]==0: 
     ...:             alist.append([i,j]) 
     ...:                                                                                            
In [535]: alist                                                                                      
Out[535]: [[0, 0], [0, 1], [0, 2], [1, 1], [2, 0], [2, 2]]

argwhere 实际上使用np.nonzero 来获取一个数组元组,这些数组索引所需的位置。

In [536]: np.nonzero(board==0)                                                                       
Out[536]: (array([0, 0, 0, 1, 2, 2]), array([0, 1, 2, 1, 0, 2]))

这个nonzero 版本通常更易于使用。例如,它可以直接用于选择所有这些单元格:

In [537]: board[np.nonzero(board==0)]                                                                
Out[537]: array([0, 0, 0, 0, 0, 0])

并将其中一些设置为 1:

In [538]: board[np.nonzero(board==0)] = np.random.randint(0,2,6)                                     
In [539]: board                                                                                      
Out[539]: 
array([[0, 0, 1],
       [1, 0, 1],
       [1, 1, 1]])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 2019-05-03
    • 1970-01-01
    • 2014-01-13
    • 2020-09-11
    • 2018-01-08
    • 2020-05-02
    相关资源
    最近更新 更多