Home > OS >  How to update dictionary arrays with list of new arrays?
How to update dictionary arrays with list of new arrays?

Time:05-01

Suppose I have this dictionary:

d = {'A': array([[ 1.33081005e-07,  1.89142917e-06, -8.26164486e-04],
                 [ 1.21548603e-06, -1.69887159e-07,  8.47602404e-03],
                 [-1.15330093e-03, -9.03091996e-03,  3.99727161e-01]]),
     'B': array([[-1.41077929e-06,  1.25641038e-05, -7.69075050e-03],
                 [-1.47192483e-05,  4.66520159e-06,  1.17920640e-02],
                 [ 7.12669880e-03, -1.59184362e-02,  4.23460368e 00]]),
     'C': array([[-2.10167616e-06, -2.33597415e-05,  1.33922129e-02],
                 [ 1.56584782e-06, -3.03106291e-06,  8.26859973e-03],
                 [-1.25428306e-03, -3.63535567e-03, -1.20946245e 00]])}

I would like to replace above arrays with these ones:

l = [array([[ 6.08673971e-08,  1.00655302e-06, -4.65288818e-04],
            [ 1.19474476e-06, -2.88789022e-09,  5.57550601e-03],
            [-9.05233574e-04, -6.06804072e-03,  2.99157094e-01]]),
     array([[-9.35906670e-07,  8.44224813e-06, -5.09319474e-03],
            [-9.78983141e-06,  2.79665275e-06,  7.48336147e-03],
            [ 4.51720619e-03, -9.90270741e-03,  2.66666555e 00]]),
     array([[-6.12320335e-07, -9.52450863e-06,  6.12536143e-03],
            [ 2.45806385e-06,  1.17814563e-05, -1.90645193e-04],
            [-1.69831099e-03, -1.18684626e-02,  3.44502533e 00]])]

What do you suggest for the best efficient way?

CodePudding user response:

Assuming d the dictionary and l the list, you can compute a new dictionary using:

d2 = dict(zip(d,l))

If you really want to update d:

d.update(dict(zip(d,l)))

Or:

for k,v in zip(d,l):
    d[k] = v

output:

{'A': array([[ 6.08673971e-08,  1.00655302e-06, -4.65288818e-04],
        [ 1.19474476e-06, -2.88789022e-09,  5.57550601e-03],
        [-9.05233574e-04, -6.06804072e-03,  2.99157094e-01]]),
 'B': array([[-9.35906670e-07,  8.44224813e-06, -5.09319474e-03],
        [-9.78983141e-06,  2.79665275e-06,  7.48336147e-03],
        [ 4.51720619e-03, -9.90270741e-03,  2.66666555e 00]]),
 'C': array([[-6.12320335e-07, -9.52450863e-06,  6.12536143e-03],
        [ 2.45806385e-06,  1.17814563e-05, -1.90645193e-04],
        [-1.69831099e-03, -1.18684626e-02,  3.44502533e 00]])}
  • Related