按照第一个字典的键对第二个字典进行排序

时间:2018-09-20 16:41:11

标签: arrays python-3.x sorting numpy dictionary

l具有三个数组,即:

ref_labels=array(['hammerthrow_g10_c07', 'wallpushups_g08_c04', 'archery_g09_c03',..., 'frisbeecatch_g09_c03', 'tabletennisshot_g12_c01',
       'surfing_g10_c03'], dtype='<U26') 

ref_labels的形状为(3000,)

ref_labels is其他两个数组的引用顺序:

to_be_ordered_labels=array(['walkingwithdog_g08_c01', 'nunchucks_g13_c02', ....,'javelinthrow_g09_c03', 'playingflute_g12_c04', 'benchpress_g12_c02', 'frisbeecatch_g14_c01', 'jumpingjack_g13_c07', 'handstandpushups_g08_c05'], dtype='<U28')

形状为(3000,)py

我还有一个浮点数的数组

to_be_ordered_arrays_of_float which is of shape (3000,101)

这里是

的示例
to_be_ordered_arrays_of_float[0]
array([6.80778456e-08, 1.58984292e-08, 2.69517453e-09, 2.82882096e-09,
       1.35314554e-06, 2.66444680e-08, 1.96892984e-06, 1.64217184e-07,
       2.40923086e-08, 2.35174169e-09, 1.45098711e-09, 2.10457629e-09,
       6.51394956e-08, 4.71427897e-10, 2.48873818e-07, 2.25375985e-08,
       1.56526866e-07, 5.60892097e-08, 1.95728759e-07, 7.24156690e-09,
       1.33053675e-06, 1.06113225e-08, 3.07328882e-08, 1.58847371e-07,
       1.85805094e-09, 4.20591455e-08, 9.77163683e-09, 5.33082073e-07,
       4.52592142e-09, 6.20161609e-06, 4.25105497e-08, 8.63415792e-08,
       1.98478956e-05, 5.02593911e-10, 9.98565793e-01, 2.76135781e-09,
       3.33678649e-08, 2.11770342e-07, 8.09025558e-09, 3.98751210e-09,
       8.28181399e-08, 9.51544799e-09, 9.00462692e-06, 3.11626500e-05,
       4.00733006e-06, 2.63792316e-07, 8.75839589e-07, 6.86739767e-08,
       1.00570272e-08, 4.86615797e-08, 2.16352909e-08, 2.04790371e-08,
       1.72958153e-07, 5.78688697e-09, 4.83830753e-09, 3.75843297e-06,
       6.00361894e-09, 8.48605123e-06, 1.46872461e-08, 2.71486789e-09,
       2.72728915e-08, 9.99970240e-09, 2.69397837e-08, 5.73341836e-08,
       3.06793368e-09, 3.16495052e-10, 5.69838967e-08, 1.04099172e-07,
       7.12405024e-09, 1.70841350e-08, 1.58363335e-07, 7.10246439e-09,
       1.65444236e-09, 3.54519578e-08, 5.11049834e-08, 9.68790381e-09,
       2.10373469e-06, 1.54864466e-09, 2.11581687e-06, 4.93066139e-08,
       1.78782467e-09, 3.54902490e-08, 1.40120218e-08, 1.82792789e-07,
       8.51292086e-08, 9.88524320e-08, 3.18586721e-08, 3.76303788e-08,
       1.85764435e-08, 6.87650381e-09, 2.80555332e-06, 2.55424425e-06,
       1.33028883e-03, 2.45268382e-07, 1.37083349e-08, 3.04683105e-08,
       1.82895951e-06, 4.65470373e-09, 6.83182293e-08, 3.18085824e-08,
       2.54011603e-08], dtype=float32)

我的问题是给定 ref_labels 中的顺序,如何重新排序 to_be_ordered_labels to_be_ordered_arrays_of_float

我尝试了什么?

我创建了一个随机数组以构建字典,其中ref_labels表示键,然后按以下顺序重新排序:

random_arrays=np.random.rand(3000,101)
dic1=dict(zip(ref_labels,random_arrays))
dic2=dict(zip(to_be_ordered_labels,to_be_ordered_arrays_of_float))
ordered_dic2=sorted(dic2.items(), key=lambda kv: dic1[kv[0]])

但是我得到以下错误: ValueError:具有多个元素的数组的真值不明确。使用a.any()或a.all()

谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

  

我的问题是给定ref_labels中的顺序,我该如何重新排序to_be_ordered_labels和to_be_ordered_arrays_of_float?

如果我理解正确,那么您要执行的操作如下:

import numpy as np
ref = np.array(['labels', 'that', 'define', 'the', 'order'])
other_labels = np.array(['other', 'labels', 'to', 'be', 'sorted'])
rand_data = np.random.randn(5, 10)
idx_sort = np.argsort(ref)
sorted_labels = other_labels[idx_sort]
rand_data = rand_data[idx_sort, :]

如果要排序的字典,则可能要检查集合库中的OrderedDict类。