I have the following 3 lists of different lengths.
a = [1, 2]
b = [1, 2, 3, 4, 5]
c = [1, 2, 3, 4]
I want to combine these three lists into a two-dimensional array, with a length not enough to fill with 0.Try the code below
import numpy as np
a = [1, 2]
b = [1, 2, 3, 4, 5]
c = [1, 2, 3, 4]
d = []
d.append(a)
d.append(b)
d.append(c)
max_len = 0
for i in d:
if len(i) > max_len:
max_len = len(i)
f = []
for i in d:
for j in range(len(i), max_len):
i.append(0)
f.append(i)
f = np.array(f)
print(f)
The expected output is as follows.
[[1 2 0 0 0]
[1 2 3 4 5]
[1 2 3 4 0]]
Is there a way to do the same thing in the numpy library? Thanks in advance.