简体   繁体   English

如何将字典中的每个值都转换为浮点数? (递归)

[英]How do I turn every value in my dictionary to a float? (recursively)

d = { 'scores': 4, 'teams': { 'yellow': 11, 'blue': 4 } } d = { 'scores': 4, 'teams': { 'yellow': 11, 'blue': 4 } }

How do I take a dictionary, and turn every integer into a float?如何获取字典,并将每个 integer 变成浮点数? Recursively, for every value max deep.递归地,对于每个最大深度的值。

def float_dict(d):
    new_dict = {}
    for k,v in d.iteritems():
        if type(v) == dict:
            new_dict[k] = float_dict(v)
        else:
            new_dict[k] = float(v)
    return new_dict


>>> d = { 'scores': 4, 'teams': { 'yellow': 11, 'blue': 4 } }
>>> print float_dict(d)
{'scores': 4.0, 'teams': {'blue': 4.0, 'yellow': 11.0}}
def to_float(my_dict):
    for i in my_dict:
        if type(my_dict[i]) == dict:
            my_dict[i] = to_float(my_dict[i])
        elif type(my_dict[i]) == int:
            my_dict[i] = float(my_dict[i])
    return my_dict
defun rec-func (dictionary &optional (current 0))
  if dictionary[current] == dict:
    rec-func (dictionary[current], 0)
  else:
    dictionary[current] = float(dictionary[current])
    rec-func (dictionary current+1)

It's a recursive function but an iterative process and it's pseudo-code.这是一个递归的 function 但一个迭代过程,它是伪代码。 Also, don't forget to put into some conditional to check if you've reached the end.另外,不要忘记输入一些条件来检查您是否已经到达终点。 Probably not the best solution, I haven't even tested it.可能不是最好的解决方案,我什至没有测试过。

Hm, the problem really is getting a specific element of a dictionary but I think my function should work... I hope.嗯,问题确实是获取字典的特定元素,但我认为我的 function 应该可以工作......我希望。

EDIT: Ooops, not properly indented, no idea how to do that though.编辑:哎呀,没有正确缩进,但不知道该怎么做。

edit (santa4nt) : Fixed it.编辑(santa4nt) :修复它。

>>> d = { 'scores': 4, 'teams': { 'yellow': 11, 'blue': 4 } }
>>> import pickle
>>> pickle.loads(pickle.dumps(d).replace("\nI","\nF"))
{'scores': 4.0, 'teams': {'blue': 4.0, 'yellow': 11.0}}

note pickle is recursive:)注意pickle是递归的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何判断字典中特定键的值是否是字典,以便我可以基于此递归解决编程问题? - How do I tell if the value of a particular key in my dictionary is a dictionary so that I can solve programming questions based on this recursively? 如何在 python 中将 a.0 浮点数转换为 integer? - How do I turn a .0 float into an integer in python? 如何将字典变成字符串? - How do I turn a dictionary into a string? 如何将列表作为值的字典转换为整数作为值的字典? - How do I turn a dictionary with lists as values into a dictionary with integers as values? 如何将sys.argv连接到浮点值? - how do I connect sys.argv into my float value? 如果我的值是元组,如何按值对字典进行排序? - How do I sort a dictionary by value if my value is a tuple? 我如何使用float来转换字典中的值字符串并使用新值进行数学运算? - how do i use float to convert a value string inside a dictionary and do math with the new values? 如何从提供的数据中递归地创建字典结构? - How do I create a dictionary structure from provided data recursively? 我的str(float)在直方图字典转换时损坏,如何停止此操作? - my str(float) gets broken on histogram dictionary conversion, how do I stop this? 如何在 for 循环中计算值作为字典键的一部分 - How do I counter value in a for loop as part of my dictionary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM