将字典映射到向量以获取一组索引向量

时间:2017-11-19 19:06:14

标签: python dictionary vector

我有以下长度为200的向量,其中包含一个参考剪辑列表,如下所示:

clips_reference_name=['v_ApplyEyeMakeup_g08_c01',
 'v_ApplyEyeMakeup_g08_c02',
 'v_ApplyEyeMakeup_g08_c03',
 'v_ApplyEyeMakeup_g08_c04',
 'v_ApplyEyeMakeup_g08_c05',
 'v_ApplyEyeMakeup_g09_c01',
 'v_ApplyEyeMakeup_g09_c02',
 'v_ApplyEyeMakeup_g09_c03',
 'v_ApplyEyeMakeup_g09_c04',
 'v_ApplyEyeMakeup_g09_c05',
 'v_ApplyEyeMakeup_g09_c06',
 'v_ApplyEyeMakeup_g09_c07',
 'v_ApplyEyeMakeup_g10_c01',
 'v_ApplyEyeMakeup_g10_c02',
 'v_ApplyEyeMakeup_g10_c03',
 'v_ApplyEyeMakeup_g10_c04',
 'v_ApplyEyeMakeup_g10_c05',
 'v_ApplyEyeMakeup_g11_c01',
 'v_ApplyEyeMakeup_g11_c02',
 'v_ApplyEyeMakeup_g11_c03',
 'v_ApplyLipstick_g08_c01',
 'v_ApplyLipstick_g08_c02',
 'v_ApplyLipstick_g08_c03',
 'v_ApplyLipstick_g08_c04',
 'v_ApplyLipstick_g09_c01',
 'v_ApplyLipstick_g09_c02',
 'v_ApplyLipstick_g09_c03',
 'v_ApplyLipstick_g09_c04',
 'v_ApplyLipstick_g10_c01',
 'v_ApplyLipstick_g10_c02',
 'v_ApplyLipstick_g10_c03',
 'v_ApplyLipstick_g10_c04',
 'v_ApplyLipstick_g11_c01',
 'v_ApplyLipstick_g11_c02',
 'v_ApplyLipstick_g11_c03',
 'v_ApplyLipstick_g11_c04',
 'v_ApplyLipstick_g12_c01',
 'v_ApplyLipstick_g12_c02',
 'v_ApplyLipstick_g12_c03',
 'v_ApplyLipstick_g12_c04',
 'v_Archery_g08_c01',
 'v_Archery_g08_c02',
 'v_Archery_g08_c03',
 'v_Archery_g08_c04',
 'v_Archery_g08_c05',
 'v_Archery_g09_c01',
 'v_Archery_g09_c02',
 'v_Archery_g09_c03',
 'v_Archery_g09_c04',
 'v_Archery_g09_c05',
 'v_Archery_g09_c06',
 'v_Archery_g09_c07',
 'v_Archery_g10_c01',
 'v_Archery_g10_c02',
 'v_Archery_g10_c03',
 'v_Archery_g10_c04',
 'v_Archery_g10_c05',
 'v_Archery_g10_c06',
 'v_Archery_g10_c07',
 'v_Archery_g11_c01',
 'v_BabyCrawling_g08_c01',
 'v_BabyCrawling_g08_c02',
 'v_BabyCrawling_g08_c03',
 'v_BabyCrawling_g08_c04',
 'v_BabyCrawling_g09_c01',
 'v_BabyCrawling_g09_c02',
 'v_BabyCrawling_g09_c03',
 'v_BabyCrawling_g09_c04',
 'v_BabyCrawling_g09_c05',
 'v_BabyCrawling_g09_c06',
 'v_BabyCrawling_g10_c01',
 'v_BabyCrawling_g10_c02',
 'v_BabyCrawling_g10_c03',
 'v_BabyCrawling_g10_c04',
 'v_BabyCrawling_g10_c05',
 'v_BabyCrawling_g11_c01',
 'v_BabyCrawling_g11_c02',
 'v_BabyCrawling_g11_c03',
 'v_BabyCrawling_g11_c04',
 'v_BabyCrawling_g12_c01',
 'v_BalanceBeam_g08_c01',
 'v_BalanceBeam_g08_c02',
 'v_BalanceBeam_g08_c03',
 'v_BalanceBeam_g08_c04',
 'v_BalanceBeam_g09_c01',
 'v_BalanceBeam_g09_c02',
 'v_BalanceBeam_g09_c03',
 'v_BalanceBeam_g09_c04',
 'v_BalanceBeam_g10_c01',
 'v_BalanceBeam_g10_c02',
 'v_BalanceBeam_g10_c03',
 'v_BalanceBeam_g10_c04',
 'v_BalanceBeam_g11_c01',
 'v_BalanceBeam_g11_c02',
 'v_BalanceBeam_g11_c03',
 'v_BalanceBeam_g11_c04',
 'v_BalanceBeam_g12_c01',
 'v_BalanceBeam_g12_c02',
 'v_BalanceBeam_g12_c03',
 'v_BandMarching_g08_c01',
 'v_BandMarching_g08_c02',
 'v_BandMarching_g08_c03',
 'v_BandMarching_g08_c04',
 'v_BandMarching_g08_c05',
 'v_BandMarching_g08_c06',
 'v_BandMarching_g08_c07',
 'v_BandMarching_g09_c01',
 'v_BandMarching_g09_c02',
 'v_BandMarching_g09_c03',
 'v_BandMarching_g09_c04',
 'v_BandMarching_g09_c05',
 'v_BandMarching_g09_c06',
 'v_BandMarching_g09_c07',
 'v_BandMarching_g10_c01',
 'v_BandMarching_g10_c02',
 'v_BandMarching_g10_c03',
 'v_BandMarching_g10_c04',
 'v_BandMarching_g10_c05',
 'v_BandMarching_g10_c06',
 'v_BandMarching_g10_c07',
 'v_BaseballPitch_g08_c01',
 'v_BaseballPitch_g08_c02',
 'v_BaseballPitch_g08_c03',
 'v_BaseballPitch_g08_c04',
 'v_BaseballPitch_g08_c05',
 'v_BaseballPitch_g08_c06',
 'v_BaseballPitch_g08_c07',
 'v_BaseballPitch_g09_c01',
 'v_BaseballPitch_g09_c02',
 'v_BaseballPitch_g09_c03',
 'v_BaseballPitch_g09_c04',
 'v_BaseballPitch_g09_c05',
 'v_BaseballPitch_g09_c06',
 'v_BaseballPitch_g09_c07',
 'v_BaseballPitch_g10_c01',
 'v_BaseballPitch_g10_c02',
 'v_BaseballPitch_g10_c03',
 'v_BaseballPitch_g10_c04',
 'v_BaseballPitch_g10_c05',
 'v_BaseballPitch_g11_c01',
 'v_Basketball_g08_c01',
 'v_Basketball_g08_c02',
 'v_Basketball_g08_c03',
 'v_Basketball_g08_c04',
 'v_Basketball_g09_c01',
 'v_Basketball_g09_c02',
 'v_Basketball_g09_c03',
 'v_Basketball_g09_c04',
 'v_Basketball_g09_c05',
 'v_Basketball_g10_c01',
 'v_Basketball_g10_c02',
 'v_Basketball_g10_c03',
 'v_Basketball_g10_c04',
 'v_Basketball_g10_c05',
 'v_Basketball_g11_c01',
 'v_Basketball_g11_c02',
 'v_Basketball_g11_c03',
 'v_Basketball_g11_c04',
 'v_Basketball_g11_c05',
 'v_Basketball_g12_c01',
 'v_BasketballDunk_g08_c01',
 'v_BasketballDunk_g08_c02',
 'v_BasketballDunk_g08_c03',
 'v_BasketballDunk_g08_c04',
 'v_BasketballDunk_g08_c05',
 'v_BasketballDunk_g09_c01',
 'v_BasketballDunk_g09_c02',
 'v_BasketballDunk_g09_c03',
 'v_BasketballDunk_g09_c04',
 'v_BasketballDunk_g09_c05',
 'v_BasketballDunk_g10_c01',
 'v_BasketballDunk_g10_c02',
 'v_BasketballDunk_g10_c03',
 'v_BasketballDunk_g10_c04',
 'v_BasketballDunk_g10_c05',
 'v_BasketballDunk_g11_c01',
 'v_BasketballDunk_g11_c02',
 'v_BasketballDunk_g11_c03',
 'v_BasketballDunk_g11_c04',
 'v_BasketballDunk_g11_c05',
 'v_BenchPress_g08_c01',
 'v_BenchPress_g08_c02',
 'v_BenchPress_g08_c03',
 'v_BenchPress_g08_c04',
 'v_BenchPress_g08_c05',
 'v_BenchPress_g08_c06',
 'v_BenchPress_g08_c07',
 'v_BenchPress_g09_c01',
 'v_BenchPress_g09_c02',
 'v_BenchPress_g09_c03',
 'v_BenchPress_g09_c04',
 'v_BenchPress_g09_c05',
 'v_BenchPress_g09_c06',
 'v_BenchPress_g09_c07',
 'v_BenchPress_g10_c01',
 'v_BenchPress_g10_c02',
 'v_BenchPress_g10_c03',
 'v_BenchPress_g10_c04',
 'v_BenchPress_g11_c01',
 'v_BenchPress_g11_c02']

每个剪辑引用名称与一组图像相关联。例如: clips_reference_name中的第一个引用。 'v_ApplyEyeMakeup_g08_c01',是 与一组图像相关联(本例中为300个图像),在以下代码中称为labels

v_ApplyEyeMakeup_g08_c01.**0001**.jpeg, ..., v_ApplyEyeMakeup_g08_c01.**0300**.jpeg,

每个参考名称的图像数量因图像而异。

我有一个框架字典(图像名称),它们的值如下:

dataset= dict(zip(labels, frames))

labels是一个列表,其值如下:

v_BasketballDunk_g08_c04_0018.jpeg
v_BandMarching_g10_c05_0097.jpeg
v_BabyCrawling_g11_c01_0010.jpeg
v_ApplyEyeMakeup_g09_c04_0148.jpeg
v_Archery_g08_c01_0008.jpeg
v_BalanceBeam_g11_c02_0058.jpeg
v_BaseballPitch_g09_c05_0002.jpeg
v_ApplyLipstick_g08_c02_0044.jpeg
v_Basketball_g11_c01_0062.jpeg
v_BenchPress_g11_c02_0012.jpeg

和帧是2048值的1-D向量。

例如:从(labels, frames)创建的字典的第一项如下:

{'v_BasketballDunk_g08_c02_0053.jpeg':
    array([ 0.88717347,  0.51302141,  0.87405699, ...,  0.41013849,
            0.38836521,  0.37444678], dtype=float32), .....}

我想要的是什么?

由于我在 clips_reference_name 中有200个项目,我想获得与每个项目相对应的200个向量,如下所示:

vector-labels_v_ApplyEyeMakeup_g08_c02 = [v_ApplyEyeMakeup_g08_c02_0001.jpeg,
                                          v_ApplyEyeMakeup_g08_c02_0002.jpeg ,
                                          ...,
                                          v_ApplyEyeMakeup_g08_c02_0300.jpeg]
 vector-frme-values_v_ApplyEyeMakeup_g08_c02 = [[0.47,...,0.98], ..., [0.17,...,0.45]]

 vector_labels-v_BabyCrawling_g09_c02 = [v_BabyCrawling_g09_c02_0001.jpeg,
                                         v_BabyCrawling_g09_c02_0002.jpeg,
                                         ...,
                                         v_BabyCrawling_g09_c02_0248.jpeg]
 vector-frme-values_v_BabyCrawling_g09_c02 = [[0.77,...,0.28], ..., [0.18,...,0.17]]

我们查找每个剪辑引用名称并查找其相应的图像: clips_reference_name+'_0001'.png, clips_reference_name+'_0002'.png ...并将它们附加到同一个向量中。

所以,最后我得到200个向量,每个向量代表剪辑引用名称的图像名称。

我没有做什么?

我坚持创建2 * 200个向量(然后每个向量的项目数量会根据描述剪辑引用名称的图像数量而变化)。

如何使用剪辑引用名称命名每个向量。向量采用整数索引而不是字符串。

我发现做一个字典,其中键表示剪辑引用名称,每个剪辑引用名称的值是与每个剪辑引用名称关联的图像集。因此,对于每个键,我们有多个值(一组标签和一组帧值(每个标签的一维矢量为2048),这些值很难操作。

1 个答案:

答案 0 :(得分:1)

如果我找到了你,那么你就会尝试按照引用名称对标签和框架进行分组,这与标签名称的第一部分相对应(直到倒数第二个_)。

然后,您可以为这两个组创建词典。

grouped_labels = {}
grouped_frames = {}

然后使用'group key'填充,如上所述。

for label, frames in dataset.items():
    key = label.rsplit('_', 1)[0]
    grouped_labels.setdefault(key, []).append(label)
    grouped_frames.setdefault(key, []).append(frames)

最后,您可以通过访问:

轻松获取群组的标签和框架
for crn in clips_reference_name:
    crn_labels = grouped_labels.get(crn, [])
    crn_frames = grouped_frames.get(crn, [])
    # do something with group's labels and frames...