0

I have a list of size N which contains lists of integers (rows) of different lengths which I'd like to convert to a N x M numpy array. I'd like to do this by adding the trailing integer X to the end of every row so that the size of the row becomes M.

The main issue is doing this very quickly.

N ~= 10^6
M ~= 4200

import numpy as np

def addTrailingZeros(data,M,X):
    for i in range(len(data)):
        data[i] = data[i]+[X]*(M-len(data[i]))
    return np.array(data, dtype=int)

data = [[0],[1],[3,3],[8,4,9,3,2,0,5]]
data2 = addTrailingZeros(data,10,0)
print('data: ',data)
print('data2: ',data2)
user1581390
  • 1,900
  • 5
  • 25
  • 38

0 Answers0