简体   繁体   English

Python中的多个列表零扩展成一个多维数组

[英]Extend a number of list zero into a multi-dimension array in Python

I am new to Python and I am trying to extend an existing list with a list of zero by a number.我是 Python 的新手,我正在尝试用一个数字扩展一个包含零列表的现有列表。 Below is my code but I believe there is another way to make it simpler and also improve the performance.下面是我的代码,但我相信还有另一种方法可以使其更简单并提高性能。

missing_len_last_slice = step - len(result_list[-1])
list = []
list_append_zero = np.pad(list, (0, len(list_channels)), 'constant')
for y in range(missing_len_last_slice):
    list.append(list_append_zero)
merge_list_result = np.vstack((result_list[-1], list))
result_list[-1] = merge_list_result

Current:当前的:

Length: 5.200长度:5.200

array([[-0.4785, -1.578 ],
       [-0.484 , -1.5815],
       [-0.483 , -1.584 ],
       ...,
       [-0.13  , -0.9475],
       [-0.117 , -0.9315],
       [-0.1175, -0.9395]])

Expectation :期望

Length: 10.000 with the extension of 4.800 [0, 0]长度:10.000,扩展为 4.800 [0, 0]

array([[-0.4785, -1.578 ],
       [-0.484 , -1.5815],
       [-0.483 , -1.584 ],
       ...,
       [-0.13  , -0.9475],
       [-0.117 , -0.9315],
       [-0.1175, -0.9395],
       [0, 0],
       [0, 0],
       ...
       [0, 0]])

PS: The number dimension of the array is dynamic. PS:数组的数字维度是动态的。 In the example, it is 2 as [-0.4785, -1.578 ] .在示例中,它是 2 作为[-0.4785, -1.578 ]

As my previous answer you can do this as:正如我之前的回答,您可以这样做:

a = np.array([[-0.4785, -1.578 ],
              [-0.484 , -1.5815],
              [-0.483 , -1.584 ]])

pad_fill = np.array([0.0, 0.0])
padding_number = 2              # 10000 - a.shape[0]

A_pad = np.pad(a, ((0, padding_number), (0, 0)), constant_values=pad_fill)
# [[-0.4785 -1.578 ]
#  [-0.484  -1.5815]
#  [-0.483  -1.584 ]
#  [ 0.      0.    ]
#  [ 0.      0.    ]]

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

相关问题 python多维列表:无法正确存储值 - python multi-dimension list: cannot store values correctly 在python中,如何使用np数组有效地处理多维? - In python, how to use np array to manipulate multi-dimension efficiently? 将多维Xarray转换为DataFrame - Python - Convert multi-dimension Xarray into DataFrame - Python 从索引列表访问多维 np.array 的元素 - Access elements of a multi-dimension np.array from a list of indices 如何在python中使用dict使用boto和Amazon EC2构建多维数组? - How can I use a dict in python to build a multi-dimension array with boto and amazon ec2? Map 多维数组中的一个元素到它的索引 - Map an element in a multi-dimension array to its index numpy 数组包含多维 numpy arrays 具有可变形状 - numpy array containing multi-dimension numpy arrays with variable shape 关于计数多维数组唯一数的输出 - regarding the output of counting the unique numbers of multi-dimension array Java中的Tensorflow:inferenceInterface.fetch转换为多维数组 - Tensorflow in Java: inferenceInterface.fetch convert to multi-dimension array 对 python 中的聚类分类数据执行多维缩放 (MDS) - Perform Multi-Dimension Scaling (MDS) for clustered categorical data in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM