根据 Numpy 数组 Python 的长度在数组末尾添加零
Adding zeroes at the end of an array based on the length the a Numpy Array Python
如果 numpy 数组的末尾低于 limit
,我将如何创建一个在其末尾添加零的函数。因此下面的 val
数组将转换为下面的预期输出。
代码:
import numpy as np
val=np.array([1,4,11])
def Adjust(limit):
#Funtion needed
Adjust(5)
Adjust(2)
Adjust(3)
Adjust(6)
预期输出:
[1,4,11,0,0]
[1,4,11]
[1,4,11]
[1,4,11,0,0,0]
def Adjust(arr, limit):
if len(arr)<limit:
return np.concatenate([arr, np.zeros(limit-len(arr), dtype = arr.dtype)])
return arr
val=np.array([1,4,11])
def Adjust(limit):
return np.concatenate([val, np.zeros(max(0, limit - len(val)))])
def dbg(limit):
print(Adjust(limit))
dbg(5)
dbg(2)
dbg(3)
dbg(6)
[0] * n
创建一个长度为 n 的零数组
a1 + a2
连接两个数组
如果 numpy 数组的末尾低于 limit
,我将如何创建一个在其末尾添加零的函数。因此下面的 val
数组将转换为下面的预期输出。
代码:
import numpy as np
val=np.array([1,4,11])
def Adjust(limit):
#Funtion needed
Adjust(5)
Adjust(2)
Adjust(3)
Adjust(6)
预期输出:
[1,4,11,0,0]
[1,4,11]
[1,4,11]
[1,4,11,0,0,0]
def Adjust(arr, limit):
if len(arr)<limit:
return np.concatenate([arr, np.zeros(limit-len(arr), dtype = arr.dtype)])
return arr
val=np.array([1,4,11])
def Adjust(limit):
return np.concatenate([val, np.zeros(max(0, limit - len(val)))])
def dbg(limit):
print(Adjust(limit))
dbg(5)
dbg(2)
dbg(3)
dbg(6)
[0] * n
创建一个长度为 n 的零数组
a1 + a2
连接两个数组