首页 > 解决方案 > 如何将嵌套列表附加到另一个列表?

问题描述

我正在尝试将嵌套列表附加到另一个相同形状的列表中,不幸的是,到目前为止,Google 和 Stackoverflow 上有关此主题的其他问题都没有提供解决方案。

timesteps = 30
keypoints = pd.read_csv('keypoints_new.csv')
print('Keypoints dataframe shape = ', keypoints.shape)
#print(keypoints)

list_dfs = []
for v in keypoints['MovementID'].unique().tolist():
    new_df = keypoints[keypoints['MovementID'] == v].drop(columns=['Frame #']).as_matrix()
    list_dfs.append(new_df)

#len(list_dfs)) - Amount of different videos:  413  
#len(list_dfs[0])) - Amount of frames for video 1:  250  
#len(list_dfs[1])) - Amount of frames for video 2:  214  
#etc...

def chunks(l, n):
    for i in range(0, len(l), n):
        yield l[i:i+n]

#Cut the list_dfs into chunks based on timesteps
timestep_dfs = []
for i in range(len(list_dfs)):
    df_timesteps = list(chunks(list_dfs[i], timesteps))
    timestep_dfs.append(df_timesteps)

#len(timestep_dfs) - Amount of different videos:  413  
#timesteps - Amount of timesteps:  30  
#len(list_dfs[0]) - Amount of frames for video 1:  250  
#len(timestep_dfs[0]) - Amount of lists within first MovementID in chunks based on timesteps:  9  
#len(timestep_dfs[0][0]) - Amount of data in first list first MovementID:  30  
#len(timestep_dfs[0][-1]) - Length of last list in a MovementID:  10

#Padding of zeros to the remaining list:
for k in range(len(timestep_dfs)):
    if len(timestep_dfs[k][-1]) < timesteps:
        zeros_list = []
        zeros_list = [0] * (len(timestep_dfs[k][0][0])-2)
        zeros_list.append(timestep_dfs[k][0][0][-2])
        zeros_list.append(timestep_dfs[k][0][0][-1])
        zeros_list = np.array(zeros_list)
        test = np.append(timestep_dfs[k][-1], zeros_list, axis=1)

#zeros_list - ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' 'Plie' 'Plie_1']
#Length of zeros_list:  77  
#Shape of zeros_list:  (77,)  
#len(timestep_dfs[k][-1][0]) - Length of a row:  77  
#timestep_dfs[k][-1][0].shape - Shape of a row:  (77,)  
#len(timestep_dfs[k][-1]) - Length of last timestep:  10  
#timestep_dfs[k][-1].shape - Shape of last timestep:  (10, 77)

所以,总结一下......我有一个 CSV,其关键点的形状为 (63564, 78)。其中有一个描述 MovementID 的列,它与我想在它自己的列表中组合的特定类型的运动的不同数量的帧(行)组合在一起。到目前为止对我来说很好,因为list_dfs给出了首选输出。

但现在棘手的部分是我将每个 MovementID 帧数量切割成一组块(基于时间步长)。因此,如果我将第一个具有 250 帧(行)的移动 ID 的时间步设置为 30,它将把它分成 9 个新列表。最后一个列表将包含少于时间步长的剩余行列表。

我的预期结果应该是这样的:

#Output of timestep_dfs[0] - So the first movementID
#Plie_1 - 250 frames - 77 columns per list:
[[Plie_1 row 1-30],
[Plie_1 row 31-60],
...
[Plie_1 row 211-240]
[Plie_1 row 241-250]]
#Apply the zeroes list:
[Plie_1 row 241-270]

第一个 MovementID 的原始输出/打印可在此处下载:https ://drive.google.com/open?id=1y6Mvu3id_rnFG4lioHB_b9cwW9zg9houX1WFL1wS488 ,如果这样更好的话。

在出现错误的第三个代码块中,我试图将那些不等于 30 的列表附加一个零列表。这对我的神经网络是必需的,它要求所有东西都是相同的形状,但可以忽略 0。现在,错误来自最后一行代码test = np.append(timestep_dfs[k][-1], zeros_list, axis=1),我似乎无法在列表中附加零列表:

ValueError:所有输入数组必须具有相同的维数

但正如我打印出来的,我试图附加的列表中的列表和零列表具有相同的形状......那么为什么我不能在那里添加它呢?

我也尝试过 np.concatenate、np.colum_stack 和 X.append,但每个都提供了自己的错误/问题,并且通过搜索,上述内容主要是正确的。

我希望以上内容是可以理解的,并且有人能够识别我的错误/错误!

标签: pythonloopsnumpynumpy-ndarray

解决方案


推荐阅读