将字典值从字符串转换为浮点数? [英] Converting dictionary values from a string to a float?

查看:370
本文介绍了将字典值从字符串转换为浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典,关键是值的标题,值是一个列表,例如:

 dict = {'  name':['  a''  b''  c'],' 价格':['  0.66' '  1.50''  0.49'],数量:['  10' '  5''  7']} 



我希望这是一个喜欢:

 dict = {'  name':['  a''  b''  c' ],'  price':[ 0  66  1  50 ,< span class =code-digit> 0 。 49 ],数量:[ 10  5  7 ]} 



其中数字是有价值的。



ps对不起,如果我有什么不对劲,我真的不知道我在做什么。



我尝试过的事情:



{k:int(v)if v.isdigit()else v for k,v in s.items()}

解决方案

使用适当的内置函数: 2。内置函数 - Python 3.3.6文档 [ ^ ]。

I have a dictionary such that the key is the title of the values and the value is a list for example:

dict = {'name':['a','b','c'], 'price':['0.66','1.50','0.49'], quantity:['10','5','7']}


and I would like this to be something like:

dict = {'name':['a','b','c'], 'price':[0.66,1.50,0.49], quantity:[10,5,7]}


where the numbers are there values.

p.s. sorry if i got anything wrong I don't really know what I'm doing.

What I have tried:

{k:int(v) if v.isdigit() else v for k,v in s.items()}

解决方案

Use the appropriate built-in function: 2. Built-in Functions — Python 3.3.6 documentation[^].


这篇关于将字典值从字符串转换为浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆