简体   繁体   中英

get a dict out of an ndarray

How can I recover a dict which was cast into a numpy ndarray ?

Ie for the following example, I want to recover test_dict from test_array :

>>> test_dict = { 'one' : 1 }
>>> test_array = np.asarray(test_dict)
>>> print repr(test_array)

array({'one': 1}, dtype=object)

These don't work:

>>> test[0]
IndexError: 0-d arrays can't be indexed

>>> dict(test)
TypeError: iteration over a 0-d array

>>> test.astype(dict)
array({'one': 1}, dtype=object)      # still in array

"Don't move your arm like that."

But anyway, I might use:

>>> test_array
array({'one': 1}, dtype=object)
>>> test_array.item()
{'one': 1}

or for that matter

>>> test_array.min()
{'one': 1}
>>> test_array.max()
{'one': 1}
>>> test_array.take(0)
{'one': 1}
>>> test_array.flat[0]
{'one': 1}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM