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