-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?

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you please add some explanation so that it would help for others to undestand

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.