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
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()
np.savez_compressed to save a few variables, including a dict, and then loading them again. I was as perplexed as OP, so this is still a relevant answer, thank you!# json & dict -> tuple
djson = json.loads('{"Foo": "bar", "Loreum": "ipsum"}') # json -> dict
dict2tupls = []
n = 0
for i in djson.items():
dict2tupls += [i]
make_nparray = np.array(dict2tupls)
print(dict2tupls)
print(make_nparray)
print(make_nparray.shape)
Dictionary:
[('Foo', 'bar'), ('Loreum', 'ipsum')]
nparray:
[['Foo', 'bar'], ['Loreum', 'ipsum']]
nparray Dimensions:
(2, 2)
This is the solution I came up with, convert keys:value into tuples. Handle dimensions accordingly..