1

I am new to Python and I am trying to extend an existing list with a list of zero by a number. 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

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]

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. In the example, it is 2 as [-0.4785, -1.578 ].

Barmar
  • 741,623
  • 53
  • 500
  • 612
Huy DQ
  • 121
  • 1
  • 11
  • Don't use `list` as a variable name, it's the name of a built-in type. – Barmar May 06 '22 at 09:56
  • Does this answer your question? [how to combine many 2d numpy arrays into 3d array with padding](https://stackoverflow.com/questions/72041873/how-to-combine-many-2d-numpy-arrays-into-3d-array-with-padding) – Ali_Sh May 06 '22 at 10:02
  • @Barmar Thanks for your recommendation. I will update the variable. – Huy DQ May 06 '22 at 10:51

1 Answers1

0

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.    ]]
Ali_Sh
  • 2,667
  • 3
  • 43
  • 66