我创建了l字典如下:
preds[0]
array([-106.56707764, -115.15104675, -114.25883484, -115.26295471,
-115.89038086, -113.02381134, -115.05940247, -114.35089874,
-114.89484406, -114.51584625, -114.10389709, -88.91298676,
-109.24221802, -108.85653687, -106.73032379, -106.2243576 ,
-108.58371735, -108.14849854, -109.69474792, -105.89745331,
-108.72554016, -111.92811584, -107.85765839, -107.41165161,
-107.29457855, -104.40960693, -106.97180176, -113.36640167,
-107.42208862, -106.724823 , -109.33970642, -106.57996368,
-108.20727539, -109.35509491, -112.19174957, -109.18234253,
-112.828125 ], dtype=float32)
然后我按照preds [0]的索引值创建了一个字典 如下:
key=range(len(pred[0]))
g=dict(zip(key,zip(preds[0])))
然后我通过preds [0]
的递减值对diction进行排序g_sorted = sorted(g, key=g.get, reverse=True)
for k in g_sorted:
print k,g[k]
我得到了以下内容:
11 (-88.912987,)
25 (-104.40961,)
19 (-105.89745,)
15 (-106.22436,)
0 (-106.56708,)
31 (-106.57996,)
29 (-106.72482,)
14 (-106.73032,)
26 (-106.9718,)
24 (-107.29458,)
23 (-107.41165,)
28 (-107.42209,)
22 (-107.85766,)
17 (-108.1485,)
32 (-108.20728,)
16 (-108.58372,)
20 (-108.72554,)
13 (-108.85654,)
35 (-109.18234,)
12 (-109.24222,)
30 (-109.33971,)
33 (-109.35509,)
18 (-109.69475,)
21 (-111.92812,)
34 (-112.19175,)
36 (-112.82812,)
5 (-113.02381,)
27 (-113.3664,)
10 (-114.1039,)
2 (-114.25883,)
7 (-114.3509,)
9 (-114.51585,)
8 (-114.89484,)
6 (-115.0594,)
1 (-115.15105,)
3 (-115.26295,)
4 (-115.89038,)
现在,我想将以下向量添加到词典中:
vocabulary= [-,0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,]
遵循条件
如果词汇表的索引值等于词典的键值,则将词汇表的值添加到词典中。例如
词汇表中的值a
具有索引11
,因此我添加了值a
到:
11 (-88.912987,)
成为:
11 (-88.912987,) : a
0 (-106.56708,) : -
36 (-112.82812,) : z
EDIT1 从@Santhosh的回答。我试图将结果存储在字典中,如下所示:
for k,v in g.items():
print ( '{0} {1} : {2}'.format(k,v,vocabulary[k]))
get_dic=dict(zip(k,v,vocabulary[k]))
我收到了以下错误:
TypeError: zip argument #1 must support iteration
然后我试了
import itertools
for k,v in g.items():
print ( '{0} {1} : {2}'.format(k,v,vocabulary[k]))
get_dic=dict(zip(itertools.repeat(k,v),vocabulary[k]))
我收到了以下错误:
TypeError: an integer is required
谢谢
答案 0 :(得分:2)
您可以执行类似
的操作vocabulary= ['-','0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','']
for k,v in g.items():
print ( '{0} {1} : {2}'.format(k,v,vocabulary[k]))
输出:
0 (-106.56708,) : -
1 (-115.15105,) : 0
2 (-114.25883,) : 1
3 (-115.26295,) : 2
4 (-115.89038,) : 3
...
...
编辑:如果你想要dict,只需尝试下面的内容
{ (k, v) : vocabulary[k] for k,v in g.items() }
输出:
{(0, (-106.56708,)): '-',
(1, (-115.15105,)): '0',
(2, (-114.25883,)): '1',
(3, (-115.26295,)): '2',
...
...
}