簡體   English   中英

按照第一個字典的鍵對第二個字典進行排序

[英]Sorting a second dictionary following the keys of the first dictionary

我有三個數組,分別是:

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_labelsto_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()

謝謝您的幫助

我的問題是如何給定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類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM