-3

all

I have a numpy array, the dimension of the element is different. for example:

[
 [1,2,3],
 [2,3,4,5,6]
 [1,2]
]

I want to adjust the element dimension, set the dimension as the largest one, and fill with 0, how can I do it?

user2155362
  • 1,657
  • 5
  • 18
  • 30

1 Answers1

0

You can create a placeholder with the desired shape first, then fill the placeholder with the data list

data = [[1,2,3], [2,3,4,5,6], [1,2]]

# create a placeholder
tmp = np.zeros((len(data), max([len(item) for item in data])))

# fill the placeholder with data
for ind, line in enumerate(data):
    tmp[ind, :len(line)] = line

However, this may not be super fast when the size of the data list is large.

meTchaikovsky
  • 7,478
  • 2
  • 15
  • 34