Dictionary in NumPy array

0 votes
How do I access the dictionary inside the array?

import numpy as np
x = np.array({'x': 2, 'y': 5})
My initial thought:

x['y']
Index Error: not a valid index

x[0]
Index Error: too many indices for array
Jan 18 in Python by ana1504.k
• 7,870 points
889 views

1 answer to this question.

0 votes
You have a 0-dimensional array of object dtype. Making this array at all is probably a mistake, but if you want to use it anyway, you can extract the dictionary by indexing the array with a tuple of no indices:

x[()]
or by calling the array's item method:

x.item()
answered Jan 18 by SDeb
• 13,160 points

Related Questions In Python

0 votes
1 answer

How to save Numpy array as image in python?

If you have matplotlib, you can do: import ...READ MORE

answered Nov 16, 2018 in Python by Nymeria
• 3,520 points
1,631 views
+2 votes
2 answers

In a list of dictionaries, how can I find the minimum calue in a common dictionary field.

There are several options. Here is a ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,720 points
64 views
+1 vote
2 answers

View onto a numpy array?

 just index it as you normally would. ...READ MORE

answered Oct 18, 2018 in Python by roberto
50 views
0 votes
1 answer
0 votes
1 answer

Printing a large numpy array

numpy.set_printoptions(threshold='nan') READ MORE

answered Jul 20, 2018 in Python by Nietzsche's daemon
• 4,260 points
104 views
0 votes
1 answer

Shift all indices in NumPy array

You can use the following : y = ...READ MORE

answered Feb 26 in Python by SDeb
• 13,160 points
307 views
0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,160 points
58 views