从0-d numpy数组恢复dict [英] recover dict from 0-d numpy array
问题描述
numpy.save()
(没有显示错误消息)保存一个字典,现在我需要恢复数据在字典中。当我加载它与 numpy.load()
它有类型( numpy.ndarray
),是0-d,所以它不再是字典,我无法访问其中的数据,0-d数组不可索引,所以做一些像 mydict = numpy.load('mydict')
mydict [0] ['some_key']
不工作。我也试过
recdict = dict(mydict)
但是这也不起作用。
为什么numpy没有警告我,当我用 numpy.save()
?
有没有办法恢复数据?
提前感谢
使用 mydict.item()
获取数组元素作为Python标量。
>>>导入numpy为np
>>>> np.save('/ tmp / data.npy',{'a':'Hi Mom!'})
>>> x = np.load('/ tmp / data.npy')
>>> x.item()
{'a':'嗨妈!'}
What happened is that I (by mistake) saved a dictionary with the command numpy.save()
(no error messages shown) and now I need to recover the data in the dictionary. When I load it with numpy.load()
it has type (numpy.ndarray
) and is 0-d, so it is not a dictionary any more and I can't access the data in it, 0-d arrays are not index-able so doing something like
mydict = numpy.load('mydict')
mydict[0]['some_key']
doesn't work. I also tried
recdict = dict(mydict)
but that didn't work either.
Why numpy didn't warn me when I saved the dictionary with numpy.save()
?
Is there a way to recover the data?
Thanks in advance!
Use mydict.item()
to obtain the array element as a Python scalar.
>>> import numpy as np
>>> np.save('/tmp/data.npy',{'a':'Hi Mom!'})
>>> x=np.load('/tmp/data.npy')
>>> x.item()
{'a': 'Hi Mom!'}
这篇关于从0-d numpy数组恢复dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!