如何使用新数组列表更新字典数组?
How to update dictionary arrays with list of new arrays?
假设我有这本字典:
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]])}
我想用这些替换上面的数组:
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]])]
对于最有效的方法,您有什么建议?
假设 d
字典和 l
列表,您可以使用以下方法计算新字典:
d2 = dict(zip(d,l))
如果真的要更新d
:
d.update(dict(zip(d,l)))
或者:
for k,v in zip(d,l):
d[k] = v
输出:
{'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]])}
假设我有这本字典:
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]])}
我想用这些替换上面的数组:
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]])]
对于最有效的方法,您有什么建议?
假设 d
字典和 l
列表,您可以使用以下方法计算新字典:
d2 = dict(zip(d,l))
如果真的要更新d
:
d.update(dict(zip(d,l)))
或者:
for k,v in zip(d,l):
d[k] = v
输出:
{'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]])}