0

I have a value, just like:

a = np.array({'a':1})

Then, I want to get the dict from a. But error happens when I use a[0]:

IndexError: too many indices for array

I have a look at the shape of a:

>>> a.shape
()

So, how to get the value from a?

3 Answers 3

2

You should create your array with list;

a = np.array([{'a':1}])
Sign up to request clarification or add additional context in comments.

1 Comment

In fact, I use numpy to save a dict. And after np.load, the value look like what I gave. The value is not created by me, but by np.load
1

You can access it like this,

import numpy

a = numpy.array({'a': 1, 'b': 2})
a.item()['a']
# 1

1 Comment

a[()] is another way of accessing the element inside an 0d array.
1

galmeriol is right. Go for as he has suggested.

>>> a.all()
{'a': 1}
>>>> a.all()['a']
1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.