簡體   English   中英

從ndarray中獲取字典

[英]get a dict out of an ndarray

我怎樣才能恢復dict其澆鑄成numpy ndarray

即,對於下面的例子,我想恢復test_dicttest_array

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

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

這些不起作用:

>>> 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

“不要那樣動胳膊。”

但是無論如何,我可能會使用:

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

或就此而言

>>> 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