简体   繁体   English

从ndarray中获取字典

[英]get a dict out of an ndarray

How can I recover a dict which was cast into a numpy ndarray ? 我怎样才能恢复dict其浇铸成numpy ndarray

Ie for the following example, I want to recover test_dict from test_array : 即,对于下面的例子,我想恢复test_dicttest_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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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