从Numpy数组中删除值

时间:2019-08-22 17:34:31

标签: arrays numpy

我正在创建一个游戏,要求玩家知道最近的食物。

我没有将每个食品的每个步骤都遍历(可能超过(1000+)),而是将我的房间分成了一个网格。

网格将所有无效ID存储在一个单元格中,搜索算法会在距离玩家最近的单元格中查找最接近的ID。

问题是:当玩家移动到新的单元格时,我需要更新网格以反映这一点。

在将玩家移至新的位置时,我无法将其从过去的牢房中移除。

一旦玩家输入了新的单元格,下面的更新功能就会执行。

此功能检查玩家ID是否出现在过去的单元格中,如果出现,则将其删除。 (不起作用)

然后将其更新为当前单元格。

最后将玩家ID写入新单元格中。

display_width = 800
display_height = 600
cell_size = 128

grid = np.empty((int(math.ceil(display_width/cell_size)),int(math.ceil(display_height/cell_size)),1),object)

def update(self):
    #Removes self from Grid Data
    if id(self) in grid[self.cell_x,self.cell_y]:
        np.delete(grid[self.cell_x,self.cell_y] ,np.argwhere(grid[self.cell_x,self.cell_y] == id(self)))

    #updates current cell
    self.cell_x = int(math.ceil(self.x // 128))
    self.cell_y = int(math.ceil(self.y // 128))

    #writes new grid data
    grid[self.cell_x,self.cell_y,0] = np.append(grid[self.cell_x,self.cell_y,0],id(self))
    print(grid)

结果是该对象同时出现在多个单元格中,因为它们过去的单元格位置未按要求清除。

1 个答案:

答案 0 :(得分:0)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html有手册。

numpy.delete()不会就地删除,而是返回一个副本,其中指定的行或列已删除。

您当前的代码未将delete()的结果分配给网格单元。只需添加缺少的作业即可。