【问题标题】:Python - Moving values in numpy array to bottomPython - 将numpy数组中的值移动到底部
【发布时间】:2022-01-16 06:03:54
【问题描述】:

我有一个如下所示的 numpy 数组:

大小可以通过改变'row_num'和'col_num'变量来改变

[[0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]]

在程序过程中,数组可能如下所示:

[[0. 0. 0. 0. 0. 0. 0.]
 [2. 2. 2. 0. 2. 0. 0.]
 [1. 1. 1. 0. 1. 0. 0.]
 [2. 2. 2. 0. 2. 0. 0.]
 [1. 1. 2. 0. 2. 0. 0.]
 [2. 2. 0. 0. 0. 0. 0.]
 [1. 1. 0. 0. 0. 1. 0.]]

我正在尝试创建一个将数字向下移动的功能,例如重力。因此,一旦函数运行,数组将如下所示:

[[0. 0. 0. 0. 0. 0. 0.]
 [2. 2. 0. 0. 0. 0. 0.]
 [1. 1. 0. 0. 0. 0. 0.]
 [2. 2. 2. 0. 2. 0. 0.]
 [1. 1. 1. 0. 1. 0. 0.]
 [2. 2. 2. 0. 2. 0. 0.]
 [1. 1. 2. 0. 2. 1. 0.]]

目前,我的函数内的代码如下所示:

    for cols in range(col_num):
    for rows in range(row_num-1):

        if board[rows][cols] == 0 and board[rows+1][cols] == 1:
            board[rows+1][cols] = 0
            board[rows][cols] = 1
            print('move down 1')
        elif board[rows][cols] == 0 and board[rows+1][cols] == 2:
            board[rows+1][cols] = 0
            board[rows][cols] = 2
            print('move down 2')

打印语句正在工作,因此满足条件,但它不会更改数组

【问题讨论】:

    标签: python arrays numpy


    【解决方案1】:

    实现所需输出的一种方法是使用 python 的内置 sort 方法。

    In [2]: data
    Out[2]:
    array([[0., 0., 0., 0., 0., 0., 0.],
           [2., 2., 2., 0., 2., 0., 0.],
           [1., 1., 1., 0., 1., 0., 0.],
           [2., 2., 2., 0., 2., 0., 0.],
           [1., 1., 2., 0., 2., 0., 0.],
           [2., 2., 0., 0., 0., 0., 0.],
           [1., 1., 0., 0., 0., 1., 0.]])
    
    In [3]: np.array([sorted(column, key=bool) for column in data.T]).T
    Out[3]:
    array([[0., 0., 0., 0., 0., 0., 0.],
           [2., 2., 0., 0., 0., 0., 0.],
           [1., 1., 0., 0., 0., 0., 0.],
           [2., 2., 2., 0., 2., 0., 0.],
           [1., 1., 1., 0., 1., 0., 0.],
           [2., 2., 2., 0., 2., 0., 0.],
           [1., 1., 2., 0., 2., 1., 0.]])
    

    这会将所有“零”之后的每个“非零”元素排序。

    【讨论】:

      【解决方案2】:

      您可以采用这种方法:

      import numpy as np
      import pandas as pd
      
      arr = np.array([[0., 0., 0., 0., 0., 0.,0.],
       [2.,2.,2.,0.,2.,0.,0.],
       [1.,1.,1.,0.,1.,0.,0.],
       [2.,2.,2.,0.,2.,0.,0.],
       [1.,1.,2.,0.,2.,0.,0.],
       [2.,2.,0.,0.,0.,0.,0.],
       [1.,1.,0.,0.,0.,1.,0.]])
       
      arr_t = np.array(list(zip(*arr)))
      print(np.array(list(zip(*[np.concatenate([line[line == 0], line[line =! 0]]) for line in arr_t]))))
      

      输出

      [[0. 0. 0. 0. 0. 0. 0.]
       [2. 2. 0. 0. 0. 0. 0.]
       [1. 1. 0. 0. 0. 0. 0.]
       [2. 2. 2. 0. 2. 0. 0.]
       [1. 1. 1. 0. 1. 0. 0.]
       [2. 2. 2. 0. 2. 0. 0.]
       [1. 1. 2. 0. 2. 1. 0.]]
      

      您转置二维数组,为每一行构建一个数组,将 0 的数组连接到所有其他数字,最后重新转置它

      【讨论】:

        【解决方案3】:

        我们可以使用argsort 来获取索引,以使数字与底部对齐的方式对给定数组进行排序,然后使用np.take_along_axis 使用索引对数组进行排序

        i = (arr != 0).argsort(axis=0)
        np.take_along_axis(arr, i, axis=0)
        

        array([[0., 0., 0., 0., 0., 0., 0.],
               [2., 2., 0., 0., 0., 0., 0.],
               [1., 1., 0., 0., 0., 0., 0.],
               [2., 2., 2., 0., 2., 0., 0.],
               [1., 1., 1., 0., 1., 0., 0.],
               [2., 2., 2., 0., 2., 0., 0.],
               [1., 1., 2., 0., 2., 1., 0.]])
        

        【讨论】:

          猜你喜欢
          • 2017-12-19
          • 2023-01-11
          • 2013-03-13
          • 2019-08-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-26
          • 1970-01-01
          相关资源
          最近更新 更多