简体   繁体   English

用零填充 3D 列表中的缺失值以创建 3D numpy 数组

[英]fill missing values in 3D list with zeros to create 3D numpy array

I have a 3D list ll which can be of size 100 K * 10 * 3我有一个100 K * 10 * 3大小的 3D 列表ll

ll = [
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10,11,12]], [[6, 7, 8],[12, 13, 14]], [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
]

I want it to be我希望它是

ll = [[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10,11,12]], [[6, 7, 8],[12, 13, 14], [0, 0, 0], [0, 0, 0]], [[10, 20, 30], [40, 50, 60], [70, 80, 90], [0,0,0]]]

so that I can create a1 = np.array(l1)这样我就可以创建a1 = np.array(l1)

a1

array([
[[1,2,3], [4,5,6], [7,8,9], [10,11,12]]
[[6,7,8], [12,13,14], [0,0,0], [0,0,0]]
[[10, 20, 30], [40, 50, 60], [70, 80, 90], [0,0,0]]
])

I have read the following but they are for 2D, i am not able to do it for 3D.我已阅读以下内容,但它们是用于 2D 的,我无法用于 3D。

https://stackoverflow.com/a/38619333/5202279 https://stackoverflow.com/a/38619333/5202279

https://stackoverflow.com/a/43149308/5202279 https://stackoverflow.com/a/43149308/5202279

Here's a way that allocates the NumPy array up front then copies the data over.这是一种预先分配 NumPy 数组然后复制数据的方法。 Assuming you don't actually need the expanded ll , this should use less memory than appending the 0-triples to ll before creating a1 :假设您实际上不需要扩展的ll ,这应该比在创建a1之前将 0 三元组附加到ll使用更少的内存:

a1 = np.zeros((len(ll), max([len(k) for k in ll]), 3))
for ctr,k in enumerate(ll):
     a1[ctr,:len(k),:] = k

a1
array([[[ 1.,  2.,  3.],
        [ 4.,  5.,  6.],
        [ 7.,  8.,  9.],
        [10., 11., 12.]],

       [[ 6.,  7.,  8.],
        [12., 13., 14.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[10., 20., 30.],
        [40., 50., 60.],
        [70., 80., 90.],
        [ 0.,  0.,  0.]]])

max([len(k) for k in ll]) tells us the maximum number of triples in any member of ll . max([len(k) for k in ll])告诉我们ll任何成员中三元组的最大数量。 We allocate a 0-initialized NumPy array of the desired size.我们分配了一个所需大小的 0 初始化 NumPy 数组。 Then in the loop, smart indexing tells us where in a1 to copy each member of ll .然后在循环中,智能索引告诉我们在a1何处复制ll每个成员。

Iterate over all elements in the list, which are also lists and get the max length.迭代列表中的所有元素,这些元素也是列表并获得最大长度。 Then append zeros to every "sublist" that does not have the max length.然后将零附加到每个没有最大长度的“子列表”。

m = max([len(k) for k in ll])

for i in range(0, len(ll)):
    while len(ll[i]) < m:
        ll[i].append([0, 0, 0])

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

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