如何将嵌套列表添加到另一个列表?

时间:2019-08-14 08:29:42

标签: python loops numpy numpy-ndarray

我正在尝试将嵌套列表添加到另一个具有相同形状的列表中,很遗憾,到目前为止,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)

因此,总结一下...我得到了一个关键点形状为(63564,78)的CSV。在其中的一列中描述了MovementID,该列与各种类型的帧(行)结合在一起,用于特定类型的运动,我希望将其合并到自己的列表中。到目前为止对我来说是一件好事,因为 list_dfs 提供了首选输出。

但是,现在最棘手的部分是,我正在按一组时间块(基于时间步长)削减每个MovementID帧的数量。因此,如果我将具有250帧(行)的第一个MovementID的时间步长设置为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,但是每个都提供了自己的错误/问题,并且通过搜索上述内容基本上应该是正确的。

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

0 个答案:

没有答案