recover dict from 0-d numpy array
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 arrays can be indexed using the empty tuple:
>>> import numpy as np>>> x = np.array({'x': 1})>>> xarray({'x': 1}, dtype=object)>>> x[()]{'x': 1}>>> type(x[()])<type 'dict'>
It's a way to recover the dict type
data that using 'allow_pickle=True'
and '.tolist()'
.
I hope it will help you.
numpy.save('mydict.npy', org_dict)#mydict = numpy.load('mydict.npy', allow_pickle=True)# convert type to dict from numpy.ndarray. _recdict = mydict.tolist() print('recdict :', type(recdict), recdict) # 'recdict' is '<type dict>'