【问题标题】:sort data of array in python to an an 4 dimensional array将python中的数组数据排序为一个4维数组
【发布时间】:2022-07-07 18:18:32
【问题描述】:

我有一个这样的数组

a = [1,2,3,4,5,6,7...........100]

输出是这样的

data[0] = [1,26,51,76]
data[1] = [2,27,52,77]
data[2] = [3,28,53,78]
data[3] = [4,29,54,79]
data[4] = [5,30,55,80]
.
.
.
.
data[24] = [25,50,75,100]

如果

a=[1,2,3,4]

输出是

data[0] =[1,2,3,4]

如果

a=[1,2,3,4,5,6,7,8]

输出是

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

如果

a=[1,2,3,4,5,6]

输出是

data[0]=[1,3,5,null]
data[1]=[2,4,6,null]

谁能帮帮我

【问题讨论】:

  • 不是 4dim 数组而是 2dim 数组。到目前为止,您尝试过什么?
  • 它不是 4 维的,它在每个数组中的 4 个值和 2 维
  • 什么是a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  • @GeekyQuentin 跟随主题,我认为这是imgur.com/a/MhTzlsW

标签: python


【解决方案1】:

所以从提供的示例中,我想我已经提出了一个原始的 Python 实现,并且为了额外的点,我使用 n 维实现了它。我不是数学家……但是因为我喜欢数学问题,所以我花了一些时间处理案例。

from math import ceil, floor

"""
Converts a 1d array into a 4d-col 2d-array.
"""
def conv(a, dims=4):
    # Pre-calculating the rows required.
    l = len(a) # length of original a
    t = ceil(l/dims) # total cols required
    r = [[None for _ in range(dims)] for _ in range(t)] # return array
    print(f"Array is of length {l}, and requires {t} rows")

    # Iterating over the length of the a-array.
    for i in range(l):
        v = a[i] # value
        # Calculating our row/col (x/y).
        x = i%t
        y = floor(i/t)
        r[x][y] = v 
    return r

# Invoke converter, then iterate through rows to display results easier.
a = conv([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
for r in a:
    print(r)

【讨论】:

    【解决方案2】:

    您可以使用 numpy 来实现这一点。在核心中,它只是用 NaN 值填充 a,直到您有足够的值来填充所需的二维结构。然后,您只需要重塑数组。注意我们先reshape成转置后的形状,然后再转置得到我们想要的形状。这样,值就会以正确的顺序出现。

    import numpy as np
    
    def get_data(a):
        # Number of columns. You can change this if you want.
        data_shape_1 = 4 
    
        # Calculate nominal number of rows
        data_shape_0 = np.ceil(a.shape[0]/float(shape_1)).astype('int')
    
        # Pad a such that it fits the 2-D "data_shape_0 x data_shape_1" array
        a_padded = np.concatenate([a, [np.nan]*(data_shape_0*data_shape_1 - a.shape[0])]) 
    
        # Reshape into transposed data shape, then re-transpose to get the right order.
        return a_padded.reshape([data_shape_1, data_shape_0]).T
    

    让我们测试您的示例(以及一个额外的示例):

    print(" First example")
    a_0 = np.arange(1,101)
    print(a_0)
    
    data_0 = get_data(a_0)
    print(data_0)
    
    print("\n Second example")
    a_1 = np.arange(1,5)
    print(a_1)
    
    data_1 = get_data(a_1)
    print(data_1)
    
    print("\n Third example")
    a_2 = np.arange(1,9)
    print(a_2)
    
    data_2 = get_data(a_2)
    print(data_2)
    
    print("\n Fourth example")
    a_2 = np.arange(1,7)
    print(a_2)
    
    data_2 = get_data(a_2)
    print(data_2)
    
    print("\n Extra example")
    a_3 = np.arange(1,15)
    print(a_3)
    
    data_3 = get_data(a_3)
    print(data_3)
    

    输出:

    First example
    [  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
      19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
      37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
      55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
      73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
      91  92  93  94  95  96  97  98  99 100]
    [[  1.  26.  51.  76.]
     [  2.  27.  52.  77.]
     [  3.  28.  53.  78.]
     [  4.  29.  54.  79.]
     [  5.  30.  55.  80.]
     [  6.  31.  56.  81.]
     [  7.  32.  57.  82.]
     [  8.  33.  58.  83.]
     [  9.  34.  59.  84.]
     [ 10.  35.  60.  85.]
     [ 11.  36.  61.  86.]
     [ 12.  37.  62.  87.]
     [ 13.  38.  63.  88.]
     [ 14.  39.  64.  89.]
     [ 15.  40.  65.  90.]
     [ 16.  41.  66.  91.]
     [ 17.  42.  67.  92.]
     [ 18.  43.  68.  93.]
     [ 19.  44.  69.  94.]
     [ 20.  45.  70.  95.]
     [ 21.  46.  71.  96.]
     [ 22.  47.  72.  97.]
     [ 23.  48.  73.  98.]
     [ 24.  49.  74.  99.]
     [ 25.  50.  75. 100.]]
    
     Second example
    [1 2 3 4]
    [[1. 2. 3. 4.]]
    
     Third example
    [1 2 3 4 5 6 7 8]
    [[1. 3. 5. 7.]
     [2. 4. 6. 8.]]
    
     Fourth example
    [1 2 3 4 5 6]
    [[ 1.  3.  5. nan]
     [ 2.  4.  6. nan]]
    
     Extra example
    [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14]
    [[ 1.  5.  9. 13.]
     [ 2.  6. 10. 14.]
     [ 3.  7. 11. nan]
     [ 4.  8. 12. nan]]
    

    在额外示例中观察 NaN 和实际值的混合列。这就是代码的行为方式,您必须确定这是否是您期望的行为。

    【讨论】:

      【解决方案3】:

      使用纯 Python 的解决方案。

      a=[1,2,3,4,5,6,7,8]
      x=-(-len(a)//4)
      
      
      data=[a[i:i+x] if len(a[i:i+x])==x else a[i:i+x]+[None]*(x-len(a[i:i+x])) for i in range(0,len(a),x)]
      data+=[[None]*x for _ in range(4-len(data))]
      data=list(zip(*data))
      

      【讨论】:

        猜你喜欢
        • 2021-10-20
        • 1970-01-01
        • 1970-01-01
        • 2018-07-26
        • 1970-01-01
        • 1970-01-01
        • 2012-04-24
        • 2021-12-28
        • 2018-10-06
        相关资源
        最近更新 更多