Created
September 19, 2012 16:48
-
-
Save nzjrs/3750722 to your computer and use it in GitHub Desktop.
Recursively convert dicts containg np types into primitive equivs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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