python - How do I turn every value in my dictionary to a float? (recursively) -
python - How do I turn every value in my dictionary to a float? (recursively) -
d = { 'scores': 4, 'teams': { 'yellow': 11, 'blue': 4 } }
how take dictionary, , turn every integer float? recursively, every value max deep.
def float_dict(d): new_dict = {} k,v in d.iteritems(): if type(v) == dict: new_dict[k] = float_dict(v) else: new_dict[k] = float(v) homecoming new_dict >>> d = { 'scores': 4, 'teams': { 'yellow': 11, 'blue': 4 } } >>> print float_dict(d) {'scores': 4.0, 'teams': {'blue': 4.0, 'yellow': 11.0}}
python dictionary
Comments
Post a Comment