如何添加数组而没有最后一个数组说'array'?

问题描述 投票:0回答:1

我将以下面描述的方式在多维数组上附加一个for循环。但是,输出不是所需的。所需的输出是一个由多个5D阵列组成的阵列。

import numpy as np

series_1 = [
             [0. 0. 0. 0. 0.],
             [0. 0. 0. 0. 0.],
             [0. 0. 0. 0. 0.]]

series_2 = [
         [0.  1.  0.  0.  0. ],
         [0.  0.5 0.5 0.  0. ],
         [0.  1.  0.  0.  0. ],
         [0.  0.  1.  0.  0. ]]

series_3 = [
         [1.  0.  0.  0.  0. ],
         [0.5 0.  0.  0.  0. ],
         [1.  0.  0.  0.  0. ],
         [1.  0.  0.  0.  0. ],
         [0.  0.  0.  1.  0. ]]

phases = [1, 2, 3]
average_total = []
for phase in phases:
    if phase == 1:
        average_series_T = series_1
    elif phase == 2:
        average_series_T = series_2
    elif phase == 3:
        average_series_T = series_3

    # add each phase to the total sequence
    average_total.append(average_series_T)


print("average total:")
print(average_total)

输出:

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

但是,我希望输出为:

    [
    [0., 0., 0., 0., 0.],
    [0., 0., 0., 0., 0.],
    [0., 0., 0., 0., 0.], 
    [0. , 1. , 0. , 0. , 0. ],
    [0. , 0.5, 0.5, 0. , 0. ],
    [0. , 1. , 0. , 0. , 0. ],
    [0. , 0. , 1. , 0. , 0. ], 
    [1. , 0. , 0. , 0. , 0. ],
    [0.5, 0. , 0. , 0. , 0. ],
    [1. , 0. , 0. , 0. , 0. ],
    [1. , 0. , 0. , 0. , 0. ],
    [0. , 0. , 0. , 1. , 0. ],
]

我希望输出是5D数组的数组,但是Pandas DataFrame或类似的东西也可以使用。我基本上想将series_1 series_2和series_3放到一个形状为(x,5)的对象中,其中x是5D数组的数量。

python arrays multidimensional-array append
1个回答
0
投票

是您想要的吗?

L_arrays=[serie_1]
L_arrays.append(serie_2)
L_arrays.append(serie_3)

result = []
for i in range(len(L_arrays)):
    for row in L_arrays[i]:
        result.append(row)

print(array(result))
© www.soinside.com 2019 - 2024. All rights reserved.