recover dict from 0-d numpy array

前端 未结 2 1673
悲哀的现实
悲哀的现实 2020-12-12 22:00

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 dictio

相关标签:
2条回答
  • 2020-12-12 22:24

    0-d arrays can be indexed using the empty tuple:

    >>> import numpy as np
    >>> x = np.array({'x': 1})
    >>> x
    array({'x': 1}, dtype=object)
    >>> x[()]
    {'x': 1}
    >>> type(x[()])
    <type 'dict'>
    
    0 讨论(0)
  • 2020-12-12 22:26

    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 讨论(0)
提交回复
热议问题