假设您需要一个平方数组,因此每行具有相同数量的项:
l=[np.NaN, 0.1, 0.4, 0.6, np.NaN, 0.8, 0.7, 0.9, np.NaN, 0.3, 0.6, 0.8]
m_l2 = np.array(l).reshape((np.isnan(l).sum(),-1))[:,1:]
意志产出:
array([[0.1, 0.4, 0.6],
[0.8, 0.7, 0.9],
[0.3, 0.6, 0.8]])
分开代码:
m_l2 = np.array(l) #Convert it to a np array from list
nan_count = np.isnan(l).sum() #Counting the amount of NaN in the array
m_l2 = m_l2.reshape((nan_count,-1)) #Reshaping it according to the amoun of NaNs as rows, with auto infering column count
m_l2 = m_l2[:,1:] #Removing the first column, which is all NaNs