Skip to content

Instantly share code, notes, and snippets.

@nzjrs nzjrs/
Created Sep 19, 2012

What would you like to do?
Recursively convert dicts containg np types into primitive equivs
import numpy as np
def fix(d):
#d = d.copy()
for k,v in d.items():
if type(v) == dict:
d[k] = fix(v)
elif isinstance(v,np.floating):
d[k] = float(v)
elif isinstance(v,np.integer):
d[k] = int(v)
return d
a = dict(foo=1,bar=np.float64(3),baz=dict(nice=1,a="string",third=np.float32(3)))
print fix(a)
print type(a['bar'])
print type(a['baz']['third'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.