Skip to content

Instantly share code, notes, and snippets.

@nzjrs
Created September 19, 2012 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nzjrs/3750722 to your computer and use it in GitHub Desktop.
Save nzjrs/3750722 to your computer and use it in GitHub Desktop.
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)
else:
pass
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