简体   繁体   English

3D 阵列中不同大小的二维 arrays 列表

[英]List of 2D arrays with different size into 3D array

I have a program that generating 2D arrays with different number of rows and appending them to list.我有一个程序可以生成具有不同行数的 2D arrays 并将它们附加到列表中。 I need to combine that list into one 3D array of 2D arrays.我需要将该列表组合成一个二维 arrays 的 3D 数组。

A = np.zeros(15).reshape(3,5)
B = np.zeros(20).reshape(4,5)
C = np.zeros(25).reshape(5,5)
lst = [A, B, C]

[array([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]]), 
 array([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]]), 
 array([[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.]])]

Needs 3D array that looks like this需要看起来像这样的 3D 阵列

[[[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. 0. 0. 0. 0.]]]

Use the tolist method:使用tolist方法:

lst = [A.tolist(), B.tolist(), C.tolist()]

Or more generic:或更通用:

lst_of_arrays = [A, B, C] # Could be initialized differently
lst = [ar.tolist() for ar in lst_of_arrays ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM