helene
helene

Reputation: 67

Add a second key to a dictionary in PYTHON

l have created l dictionary as follow :

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)

l created then a dictionary following the index values of preds[0] as follow :

key=range(len(pred[0]))
g=dict(zip(key,zip(preds[0])))

Then l sorted the diction nay by a decreasing values of preds[0]

g_sorted = sorted(g, key=g.get, reverse=True)

for k in g_sorted:
    print k,g[k]

l got the following :

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,)

Now, l would like to add the following vector to the dictionnary :

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,]

following the condition

if the index value of vocabulary equals the key value of dictionary add the value of vocabulary to dictionary . for example

value a in vocabulary has index 11 thus l add the value a

to :

11 (-88.912987,)

to become :

11 (-88.912987,) : a
0 (-106.56708,)  : -
36 (-112.82812,) : z

EDIT1 From the answer of @Santhosh. l tried to store the results in a dictionary as follow :

for k,v in g.items():
    print ( '{0} {1} : {2}'.format(k,v,vocabulary[k]))
    get_dic=dict(zip(k,v,vocabulary[k]))

l got the following error :

  TypeError: zip argument #1 must support iteration

then l tried

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]))

l got the following error :

TypeError: an integer is required

Thank you

Upvotes: 1

Views: 452

Answers (1)

Transhuman
Transhuman

Reputation: 3547

You can do something like

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]))

Output:

0 (-106.56708,) : -
1 (-115.15105,) : 0
2 (-114.25883,) : 1
3 (-115.26295,) : 2
4 (-115.89038,) : 3
...
...

EDIT : If you want in dict, just try something like below

{ (k, v) : vocabulary[k] for k,v in g.items() }

Output:

{(0, (-106.56708,)): '-',
 (1, (-115.15105,)): '0',
 (2, (-114.25883,)): '1',
 (3, (-115.26295,)): '2',
 ...
 ...
}

Upvotes: 2

Related Questions