我想从字典值中删除外部列表,但是我只能the unpacking method不确定如何在字典值中使用此列表。
我的数据看起来像这样
DIC
{'A': array([[ 1.90769404e-01, 1.26112014e-01, -2.17013955e-02]],
'B': array([[ 2.80194253e-01, 1.19333006e-01, 3.63824964e-02]],
'C': array([[ 1.40285566e-01, 4.76801395e-02, 5.49828596e-02]]}
我想删除外部列表以使它们像这样
{'A': array([ 1.90769404e-01, 1.26112014e-01, -2.17013955e-02],
'B': array([ 2.80194253e-01, 1.19333006e-01, 3.63824964e-02],
'C': array([ 1.40285566e-01, 4.76801395e-02, 5.49828596e-02]}
任何建议如何做到这一点?在此先感谢!
答案 0 :(得分:1)
当然要使用索引:
>>> {k:v[0] for k,v in DIC.items()}
{'A': array([ 0.1907694 , 0.12611201, -0.0217014 ]), 'B': array([ 0.28019425, 0.11933301, 0.0363825 ]), 'C': array([ 0.14028557, 0.04768014, 0.05498286])}
>>>
答案 1 :(得分:0)
Python字典解决方案:
{i:j[0] for i,j in DICTIONARY.items() }
#
{'A': [0.190769404, 0.126112014, -0.0217013955],
'B': [0.280194253, 0.119333006, 0.0363824964],
'C': [0.140285566, 0.0476801395, 0.0549828596]}
Python Numpy Array的解决方案
arr = {'A': np.array([[ 1.90769404e-01, 1.26112014e-01, -2.17013955e-02]]),
'B': np.array([[ 2.80194253e-01, 1.19333006e-01, 3.63824964e-02]]),
'C': np.array([[ 1.40285566e-01, 4.76801395e-02, 5.49828596e-02]])}
print({k:v[0].ravel() for k,v in arr.items()})
{'A': array([ 0.1907694 , 0.12611201, -0.0217014 ]), 'B': array([0.28019425, 0.11933301, 0.0363825 ]), 'C': array([0.14028557, 0.04768014, 0.05498286])}