Example 1: python how to pad a numpy array with zeros
# Basic syntax:
# For 1D array:
np.pad(numpy_array, (pad_left, pad_right), 'constant')
# For 2D array:
np.pad(numpy_array, ((pad_top, pad_bottom), (pad_left, pad_right)), 'constant')
# Example usage:
import numpy as np
numpy_array = np.array([1,2,3,4,5]) # 1D array
np.pad(numpy_array, (2, 3), 'constant') # Pad 2 left, 3 right
--> array([0, 0, 1, 2, 3, 4, 5, 0, 0, 0])
numpy_array = np.array([[1,2],[3,4]]) # 2D array
array([[1, 2], # Before padding
[3, 4]])
np.pad(numpy_array, ((1,2),(2,1)), 'constant') # Pad 1 top, 2 bottom, 2 left, 1 right
array([[0, 0, 0, 0, 0], # After padding
[0, 0, 1, 2, 0],
[0, 0, 3, 4, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
Example 2: 2d array python
array = [[value] * lenght] * height
array = [[0] * 5] * 10
print(array)
Example 3: np.pad
>>> 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.]])
Example 4: 2D array python
# 2D arrays in python can be used to create rudimentary games
array_2d = [['row0, column0'], ['row0, column1'], ['row0, column2'],
['row1, column0'], ['row1, column1'], ['row1, column2'],
['row2, column0'], ['row2, column1'], ['row2, column2']]