python - Changing String to Floating Point Number in Dictionary
问题描述
I am trying to convert a list made up of strings in a dictionary to float variables.
Here is the code I was given:
data = {'year':[1990, 2000, 2010], 'gdp':['8.95', '12.56', '14.78']}
The end result is supposed to look like this:
data = {'year':[1990, 2000, 2010], 'gdp':[8.95, 12.56, 14.78]}
Thank you in advance for any hints or help!
解决方案
This should do it:
data = {'year':[1990, 2000, 2010], 'gdp':['8.95', '12.56', '14.78']}
data['gdp'] = [float(string) for string in data['gdp']]
推荐阅读
- java - 在 Dataflow 模板中调用 waitUntilFinish() 之后可以运行代码吗?
- cgal - 是否有一个 CGAL 函数可以检查一个点是否在带孔的线性多边形内?
- r - 闪亮的 inputDate 和 DT::dataTableOutput
- python - 'for' 'in' 'not in' 的代码不起作用,如何解决它以尊重在命令之前给出的答案?
- javascript - 如何将值绑定到 vue.js 中的自定义输入?
- javascript - 在 fadeOut() 之后,内容再次淡入
- node.js - 在 Node.js 中使用 Mongoose 更新嵌套字段
- c# - 在 C# win 表单中使用 word 将字符串拆分为新行
- python - 从所述笔记本中关闭一个 jupyter 笔记本
- r - 将 3D 激光雷达点从笛卡尔坐标系转换为球坐标系和立体投影