如何将dict值转换为浮点数 [英] How to convert dict value to a float
本文介绍了如何将dict值转换为浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将dict值转换为浮点数
How to convert dict value to a float
dict1= {'CNN': '0.000002'}
s=dict1.values()
print (s)
print (type(s))
我得到的是:
dict_values(['0.000002'])
<class 'dict_values'> # type, but need it to be float
但是我想要的是如下的浮点值:
but what I want is the float value as below:
0.000002
<class 'float'> # needed type
推荐答案
这里有两件事:首先,s实际上是对字典值的迭代器,而不是值本身的迭代器.其次,一旦您提取了值,例如通过for循环.好消息是您可以做到这一点:
Two things here: firstly s is, in effect, an iterator over the dictionary values, not the values themselves. Secondly, once you have extracted the value, e.g. by a for loop.The good news is you can do this is one line:
print(float([x for x in s][0]))
这篇关于如何将dict值转换为浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文