2017-06-15 45 views
0

请问我对Python有点新,它一直很好,我可以评论说Python是非常性感的,直到我需要移动我想用于构建2048游戏的4x4矩阵的内容游戏的试玩here我有这个功能Python:理由NumPy数组

def cover_left(matrix): 
     new=[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] 
     for i in range(4): 
      count=0 
      for j in range(4): 
       if mat[i][j]!=0: 
        new[i][count]=mat[i][j] 
        count+=1 
     return new 

这是此功能对,如果你这样称呼它

cover_left([ 
       [1,0,2,0], 
       [3,0,4,0], 
       [5,0,6,0], 
       [0,7,0,8] 
      ]) 

它将覆盖零到左侧,并产生

[ [1, 2, 0, 0], 
    [3, 4, 0, 0], 
    [5, 6, 0, 0], 
    [7, 8, 0, 0]] 

请我需要有人帮我做这个,我相信会更快,需要更少的代码的numpy方式(我使用的深度优先搜索算法中),更重要的cover_up实施,cover_down

`cover_left`. 
`cover_up` 
    [ [1, 7, 2, 8], 
     [3, 0, 4, 0], 
     [5, 0, 6, 0], 
     [0, 0, 0, 0]] 
`cover_down` 
    [ [0, 0, 0, 0], 
     [1, 0, 2, 0], 
     [3, 0, 4, 0], 
     [5, 7, 6, 8]] 
`cover_right` 
    [ [0, 0, 1, 2], 
     [0, 0, 3, 4], 
     [0, 0, 5, 6], 
     [0, 0, 7, 8]] 

在此先感谢。

回答

4

这是一个被this other post启发和推广一个量化的方法来覆盖non-zeros所有四个方向 -

def justify(a, invalid_val=0, axis=1, side='left'):  
    """ 
    Justifies a 2D array 

    Parameters 
    ---------- 
    A : ndarray 
     Input array to be justified 
    axis : int 
     Axis along which justification is to be made 
    side : str 
     Direction of justification. It could be 'left', 'right', 'up', 'down' 
     It should be 'left' or 'right' for axis=1 and 'up' or 'down' for axis=0. 

    """ 

    if invalid_val is np.nan: 
     mask = ~np.isnan(a) 
    else: 
     mask = a!=invalid_val 
    justified_mask = np.sort(mask,axis=axis) 
    if (side=='up') | (side=='left'): 
     justified_mask = np.flip(justified_mask,axis=axis) 
    out = np.full(a.shape, invalid_val) 
    if axis==1: 
     out[justified_mask] = a[mask] 
    else: 
     out.T[justified_mask.T] = a.T[mask.T] 
    return out 

样品试验 -

In [473]: a # input array 
Out[473]: 
array([[1, 0, 2, 0], 
     [3, 0, 4, 0], 
     [5, 0, 6, 0], 
     [6, 7, 0, 8]]) 

In [474]: justify(a, axis=0, side='up') 
Out[474]: 
array([[1, 7, 2, 8], 
     [3, 0, 4, 0], 
     [5, 0, 6, 0], 
     [6, 0, 0, 0]]) 

In [475]: justify(a, axis=0, side='down') 
Out[475]: 
array([[1, 0, 0, 0], 
     [3, 0, 2, 0], 
     [5, 0, 4, 0], 
     [6, 7, 6, 8]]) 

In [476]: justify(a, axis=1, side='left') 
Out[476]: 
array([[1, 2, 0, 0], 
     [3, 4, 0, 0], 
     [5, 6, 0, 0], 
     [6, 7, 8, 0]]) 

In [477]: justify(a, axis=1, side='right') 
Out[477]: 
array([[0, 0, 1, 2], 
     [0, 0, 3, 4], 
     [0, 0, 5, 6], 
     [0, 6, 7, 8]]) 
1

感谢这一切都是我后来用

def justify(a, direction): 
    mask = a>0 
    justified_mask = numpy.sort(mask,0) if direction == 'up' or direction =='down' else numpy.sort(mask, 1) 
    if direction == 'up': 
     justified_mask = justified_mask[::-1] 
    if direction =='left': 
     justified_mask = justified_mask[:,::-1] 
    if direction =='right': 
     justified_mask = justified_mask[::-1, :]  
    out = numpy.zeros_like(a) 
    out.T[justified_mask.T] = a.T[mask.T] 
    return out 
+0

这与['other post']基本相同(https://stackoverflow.com/a/4455 9180/3293881),除了你有四个条件语句。这里有什么新东西? – Divakar

+0

签名是不同的,而且他修改了他的答案......它不是像 –

+0

之前那样的签名?一个输入参数有四个输入选项,所以有四个条件语句。另一篇文章对两个输入参数有两个输入选项。基本上是一样的。 – Divakar