如何用另一个列表附加嵌套列表?

2024-04-26 06:34:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图附加一个嵌套列表和另一个相同形状的列表,不幸的是到目前为止,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\u dfs给出了首选输出。你知道吗

但现在棘手的部分是,我要将每个MovementID的帧数剪切成一组块(基于时间步长)。因此,如果我将第一个movementID的timesteps设置为30,它有250帧(行),它将把它分成9个新列表。其中,作为最后一个列表,将包含小于timesteps的剩余行列表。你知道吗

我的预期结果应该是:

#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,如果这样更好的话。你知道吗

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

ValueError: all the input arrays must have same number of dimensions

但正如我打印出来的,我试图附加的列表中的列表和零列表有相同的形状。。。。为什么我不能把它加进去呢?你知道吗

我也试过了np.连接, np.column\u堆栈只是X.append,但是每个都提供了自己的错误/问题,通过搜索,上面的内容应该主要是正确的。你知道吗

我希望以上是可以理解的,有人能够认识到我的错误!你知道吗


Tags: ofin列表forlenzerosamountlist