# others - python - 零填充numpy数组

. ..

A = np.array([1,2,3,4,5])

pad(A, 8) # expected : [1,2,3,4,5,0,0,0]

A = np.array([1,2,3,4,5])

np.pad(A, (2, 3), 'constant')

# array([0, 0, 1, 2, 3, 4, 5, 0, 0, 0])

A = np.array([[1,2],[3,4]])

#array([[0, 0, 0, 0, 0], # 1 zero padded to the top

# [0, 0, 1, 2, 0], # 2 zeros padded to the bottom

# [0, 0, 3, 4, 0], # 2 zeros padded to the left

# [0, 0, 0, 0, 0], # 1 zero padded to the right

# [0, 0, 0, 0, 0]])

B = np.pad(A, (0, 1024 - len(A)%1024), 'constant')

B

# array([1, 2, 3,. .., 0, 0, 0])

len(B)

# 1024

A = np.ones(3000)

B = np.pad(A, (0, 1024 - len(A)%1024), 'constant')

B

# array([ 1., 1., 1.,. .., 0., 0., 0.])

len(B)

# 3072

arr = np.zeros(length)

arr[:len(A)] = A

return arr

n, remainder = divmod(len(A), 1024)

n += bool(remainder)

n, remainder = divmod(len(A), 1024)

n += bool(remainder)

arr = np.zeros(n * 1024)

arr[:len(A)] = A

return arr

t = size - len(A)

padarray([1,2,3], 8) # [1 2 3 0 0 0 0 0]

>>> A = np.array([1,2,3,4,5])

>>> npad = 8 - len(A)

array([1, 2, 3, 4, 5, 0, 0, 0])