Python 如何用零填充 numpy 数组

我想知道如何使用 python2.6.6和 numpy 1.5.0来填充一个零的2D numpy 数组。但这就是我的局限。因此,我不能使用 np.pad。例如,我想用零填充 a,使其形状匹配 b。我之所以想这么做是因为我可以这么做:

b-a

这样

>>> a
array([[ 1.,  1.,  1.,  1.,  1.],
[ 1.,  1.,  1.,  1.,  1.],
[ 1.,  1.,  1.,  1.,  1.]])
>>> b
array([[ 3.,  3.,  3.,  3.,  3.,  3.],
[ 3.,  3.,  3.,  3.,  3.,  3.],
[ 3.,  3.,  3.,  3.,  3.,  3.],
[ 3.,  3.,  3.,  3.,  3.,  3.]])
>>> c
array([[1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0]])

我能想到的唯一方法就是附加,不过这看起来相当丑陋。有没有可能使用 b.shape的清洁解决方案?

编辑, 谢谢您对 MSeifert 的回答。我不得不清理了一下,这是我得到的:

def pad(array, reference_shape, offsets):
"""
array: Array to be padded
reference_shape: tuple of size of ndarray to create
offsets: list of offsets (number of elements must be equal to the dimension of the array)
will throw a ValueError if offsets is too big and the reference_shape cannot handle the offsets
"""


# Create an array of zeros with the reference shape
result = np.zeros(reference_shape)
# Create a list of slices from offset to offset + shape in each dimension
insertHere = [slice(offsets[dim], offsets[dim] + array.shape[dim]) for dim in range(array.ndim)]
# Insert the array in the result at the specified offsets
result[insertHere] = array
return result
303680 次浏览

非常简单,使用引用形状创建一个包含零的数组:

result = np.zeros(b.shape)
# actually you can also use result = np.zeros_like(b)
# but that also copies the dtype not only the shape

然后在需要的地方插入数组:

result[:a.shape[0],:a.shape[1]] = a

瞧,你已经把它填满了:

print(result)
array([[ 1.,  1.,  1.,  1.,  1.,  0.],
[ 1.,  1.,  1.,  1.,  1.,  0.],
[ 1.,  1.,  1.,  1.,  1.,  0.],
[ 0.,  0.,  0.,  0.,  0.,  0.]])

如果定义左上角元素应该插入的位置,还可以使它更一般化一些

result = np.zeros_like(b)
x_offset = 1  # 0 would be what you wanted
y_offset = 1  # 0 in your case
result[x_offset:a.shape[0]+x_offset,y_offset:a.shape[1]+y_offset] = a
result


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

但是要小心,不要有大于允许的偏移量。例如,对于 x_offset = 2,这将会失败。


如果您有一个任意数量的维度,您可以定义一个切片列表来插入原始数组。我发现这很有趣,并创建了一个填充函数,可以填充(偏移量)一个任意形状的数组,只要数组和引用具有相同的维数和偏移量不太大。

def pad(array, reference, offsets):
"""
array: Array to be padded
reference: Reference array with the desired shape
offsets: list of offsets (number of elements must be equal to the dimension of the array)
"""
# Create an array of zeros with the reference shape
result = np.zeros(reference.shape)
# Create a list of slices from offset to offset + shape in each dimension
insertHere = [slice(offset[dim], offset[dim] + array.shape[dim]) for dim in range(a.ndim)]
# Insert the array in the result at the specified offsets
result[insertHere] = a
return result

还有一些测试案例:

import numpy as np


# 1 Dimension
a = np.ones(2)
b = np.ones(5)
offset = [3]
pad(a, b, offset)


# 3 Dimensions


a = np.ones((3,3,3))
b = np.ones((5,4,3))
offset = [1,0,0]
pad(a, b, offset)

我理解您的主要问题是需要计算 d=b-a,但是您的数组有不同的大小。不需要中间填充的 c

你可以不用填充来解决这个问题:

import numpy as np


a = np.array([[ 1.,  1.,  1.,  1.,  1.],
[ 1.,  1.,  1.,  1.,  1.],
[ 1.,  1.,  1.,  1.,  1.]])


b = np.array([[ 3.,  3.,  3.,  3.,  3.,  3.],
[ 3.,  3.,  3.,  3.,  3.,  3.],
[ 3.,  3.,  3.,  3.,  3.,  3.],
[ 3.,  3.,  3.,  3.,  3.,  3.]])


d = b.copy()
d[:a.shape[0],:a.shape[1]] -=  a


print d

产出:

[[ 2.  2.  2.  2.  2.  3.]
[ 2.  2.  2.  2.  2.  3.]
[ 2.  2.  2.  2.  2.  3.]
[ 3.  3.  3.  3.  3.  3.]]

NumPy 1.7.0(当 numpy.pad被添加的时候)现在已经很老了(它是在2013年发布的) ,所以即使问到 没有使用的功能是如何实现的,我认为知道如何使用 numpy.pad是有用的。

其实很简单:

>>> import numpy as np
>>> a = np.array([[ 1.,  1.,  1.,  1.,  1.],
...               [ 1.,  1.,  1.,  1.,  1.],
...               [ 1.,  1.,  1.,  1.,  1.]])
>>> np.pad(a, [(0, 1), (0, 1)], mode='constant')
array([[ 1.,  1.,  1.,  1.,  1.,  0.],
[ 1.,  1.,  1.,  1.,  1.,  0.],
[ 1.,  1.,  1.,  1.,  1.,  0.],
[ 0.,  0.,  0.,  0.,  0.,  0.]])

在本例中,我使用了 0作为 mode='constant'的默认值。但它也可以通过明确地传递给:

>>> np.pad(a, [(0, 1), (0, 1)], mode='constant', constant_values=0)
array([[ 1.,  1.,  1.,  1.,  1.,  0.],
[ 1.,  1.,  1.,  1.,  1.,  0.],
[ 1.,  1.,  1.,  1.,  1.,  0.],
[ 0.,  0.,  0.,  0.,  0.,  0.]])

为了防止第二个参数([(0, 1), (0, 1)])看起来令人困惑: 每个列表项(在本例中是元组)对应一个维度,其中的项表示填充 之前(第一个元素)和 之后(第二个元素)。所以:

[(0, 1), (0, 1)]
^^^^^^------ padding for second dimension
^^^^^^-------------- padding for first dimension


^------------------ no padding at the beginning of the first axis
^--------------- pad with one "value" at the end of the first axis.

在这种情况下,第一个和第二个轴的填充是相同的,所以也可以只传递2元组:

>>> np.pad(a, (0, 1), mode='constant')
array([[ 1.,  1.,  1.,  1.,  1.,  0.],
[ 1.,  1.,  1.,  1.,  1.,  0.],
[ 1.,  1.,  1.,  1.,  1.,  0.],
[ 0.,  0.,  0.,  0.,  0.,  0.]])

如果之前和之后的填充是相同的,甚至可以省略元组(但不适用于这种情况) :

>>> np.pad(a, 1, mode='constant')
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
[ 0.,  1.,  1.,  1.,  1.,  1.,  0.],
[ 0.,  1.,  1.,  1.,  1.,  1.,  0.],
[ 0.,  1.,  1.,  1.,  1.,  1.,  0.],
[ 0.,  0.,  0.,  0.,  0.,  0.,  0.]])

或者,如果在轴之前和之后的填充是相同的,但是不同,你也可以省略内部元组中的第二个参数:

>>> np.pad(a, [(1, ), (2, )], mode='constant')
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
[ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],
[ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],
[ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],
[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

然而,我倾向于总是使用明确的,因为它很容易犯错误(当 NumPys 的期望与你的意图不同时) :

>>> np.pad(a, [1, 2], mode='constant')
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
[ 0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],
[ 0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],
[ 0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],
[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

在这里 NumPy 认为你想填充所有的轴与1元素之前和2元素之后,每个轴!即使您打算用轴1中的1个元素和轴2中的2个元素来填充它。

我使用元组列表作为填充,注意这只是“我的约定”,你也可以使用列表列表或元组列表,甚至数组列表。NumPy 只检查参数的长度(或者它没有长度)和每个项的长度(或者它有长度) !

如果你需要在一个数组中添加一个由1组成的栅栏:

>>> mat = np.zeros((4,4), np.int32)
>>> mat
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
>>> mat[0,:] = mat[:,0] = mat[:,-1] =  mat[-1,:] = 1
>>> mat
array([[1, 1, 1, 1],
[1, 0, 0, 1],
[1, 0, 0, 1],
[1, 1, 1, 1]])

我知道我有点晚了,但是如果您想执行相对填充(又名边缘填充) ,这里是您可以实现它的方法。请注意,赋值的第一个实例会导致零填充,因此您可以将其用于零填充和相对填充(这是您将原始数组的边缘值复制到填充数组的地方)。

def replicate_padding(arr):
"""Perform replicate padding on a numpy array."""
new_pad_shape = tuple(np.array(arr.shape) + 2) # 2 indicates the width + height to change, a (512, 512) image --> (514, 514) padded image.
padded_array = np.zeros(new_pad_shape) #create an array of zeros with new dimensions
    

# perform replication
padded_array[1:-1,1:-1] = arr        # result will be zero-pad
padded_array[0,1:-1] = arr[0]        # perform edge pad for top row
padded_array[-1, 1:-1] = arr[-1]     # edge pad for bottom row
padded_array.T[0, 1:-1] = arr.T[0]   # edge pad for first column
padded_array.T[-1, 1:-1] = arr.T[-1] # edge pad for last column
    

#at this point, all values except for the 4 corners should have been replicated
padded_array[0][0] = arr[0][0]     # top left corner
padded_array[-1][0] = arr[-1][0]   # bottom left corner
padded_array[0][-1] = arr[0][-1]   # top right corner
padded_array[-1][-1] = arr[-1][-1] # bottom right corner


return padded_array

复杂性分析:

这个问题的最佳解决方案是 numpy 的垫法。 平均运行5次后,带有相对填充的 np.pad 只比上面定义的函数好 8%。这表明,这是一个相对和零填充填充相当优化的方法。


#My method, replicate_padding
start = time.time()
padded = replicate_padding(input_image)
end = time.time()
delta0 = end - start


#np.pad with edge padding
start = time.time()
padded = np.pad(input_image, 1, mode='edge')
end = time.time()
delta = end - start




print(delta0) # np Output: 0.0008790493011474609
print(delta)  # My Output: 0.0008130073547363281
print(100*((delta0-delta)/delta)) # Percent difference: 8.12316715542522%

Tensorflow 还实现了调整/填充图像的功能 Tf.image. pad TFF 垫

padded_image = tf.image.pad_to_bounding_box(image, top_padding, left_padding, target_height, target_width)


padded_image = tf.pad(image, paddings, "CONSTANT")

这些函数的工作方式就像张量流的其他输入-流水线特性一样,对于机器学习应用来说效果会更好。

DR

def pad_n_cols_left_of_2d_matrix(arr, n):
"""Adds n columns of zeros to left of 2D numpy array matrix.


:param arr: A two dimensional numpy array that is padded.
:param n: the number of columns that are added to the left of the matrix.
"""
padded_array = np.zeros((arr.shape[0], arr.shape[1] + n))
padded_array[:, n:] = arr
return padded_array




def pad_n_cols_right_of_2d_matrix(arr, n):
"""Adds n columns of zeros to right of 2D numpy array matrix.


:param arr: A two dimensional numpy array that is padded.
:param n: the number of columns that are added to the right of the matrix.
"""
padded_array = np.zeros((arr.shape[0], arr.shape[1] + n))
padded_array[:, : arr.shape[1]] = arr
return padded_array




def pad_n_rows_above_2d_matrix(arr, n):
"""Adds n rows of zeros above 2D numpy array matrix.


:param arr: A two dimensional numpy array that is padded.
:param n: the number of rows that are added above the matrix.
"""
padded_array = np.zeros((arr.shape[0] + n, arr.shape[1]))
padded_array[n:, :] = arr
return padded_array




def pad_n_rows_below_2d_matrix(arr, n):
"""Adds n rows of zeros below 2D numpy array matrix.


:param arr: A two dimensional numpy array that is padded.
:param n: the number of rows that are added below the matrix.
"""
padded_array = np.zeros((arr.shape[0] + n, arr.shape[1]))
padded_array[: arr.shape[0], :] = arr
return padded_array

输出

Original array:
[[0.  0.5 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.5 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.4 0.6 0.8 1.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.4 0.6 0.8 1.  0.  0.  0.  0.  0.  0. ]]
Pad left:
[[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.5 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.5 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.4 0.6 0.8 1.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.4 0.6 0.8 1.  0.  0.  0.  0.  0.  0. ]]
Pad right:
[[0.  0.5 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.5 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.4 0.6 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.4 0.6 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]]
Pad above:
[[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.5 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.5 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.4 0.6 0.8 1.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.4 0.6 0.8 1.  0.  0.  0.  0.  0.  0. ]]
Pad below:
[[0.  0.5 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.5 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.3 0.7 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.4 0.6 0.8 1.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.5 0.8 1.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.2 0.4 0.6 0.8 1.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]
[0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. ]]

动机

我来这里寻找如何填充一个数组,并找到了大量的文本,即使我的目标,像问题很简单: 填充一个2D 数组与 n行或列。 我认为解释更好,因为它们有助于建立理解。然而,只是为了让一些人节省一些时间,如果他们喜欢,这里有一个复制可粘贴的功能。

代码描述

每个函数都将 n行或列添加到传入数组中。代码假定传入的数组是一个2D 数字数组。填充的方向取决于函数定义/名称。关于更详细的解释,我想让读者参考 MSeifert 的答案。