NumPy решение на основе
# reverse the columns
a_rev = a[:,::-1]
# indices of all values after first nan to True
m = np.cumsum(~np.isnan(a_rev), 1) >= 1
# indices where to split
s = np.cumsum(m.sum(1))
# reverse again
ix = m[:,::-1]
# split to obtain n arrays
res = np.split(a[ix], s)[:-1]
print(res)
[array([nan, 1., 2.]),
array([nan, 3.]),
array([nan, 4., 5., 6.]),
array([nan, 7.])]
Чтобы получить вложенный список:
list(map(list, res))
[[nan, 1.0, 2.0], [nan, 3.0], [nan, 4.0, 5.0, 6.0], [nan, 7.0]]
Настройка:
import numpy as np
nan = np.nan
a = np.array([[nan, 1., 2., nan],
[nan, 3., nan, nan],
[nan, 4., 5., 6.],
[nan, 7., nan, nan]])
Использование списки
l = [[nan, 1., 2., nan], [nan, 3., nan, nan],
[nan, 4., 5., 6.], [nan, 7., nan, nan]]
out = []
for i in l:
i_rev = i[::-1]
for ix, j in enumerate(i_rev):
if j == j:
out.append(i[:len(i)-ix])
break
out
# [[nan, 1.0, 2.0], [nan, 3.0], [nan, 4.0, 5.0, 6.0], [nan, 7.0]]