在Python中将字符串列表转换为int(或双精度) [英] Converting a list of strings to ints (or doubles) in Python

查看:697
本文介绍了在Python中将字符串列表转换为int(或双精度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多类似于这样的字符串列表:

I have a lots of list of strings that look similar to this:

list = ['4', '-5', '5.763', '6.423', '-5', '-6.77', '10']

我想把它转换成一个int(或双精度)列表,但是$ - 不断产生错误。

I want to convert it to a list of ints (or doubles) but the - keeps producing an error.

推荐答案

>>> lst = ['4', '-5', '5.763', '6.423', '-5', '-6.77', '10']
>>> map(float, lst)
[4.0, -5.0, 5.763, 6.423, -5.0, -6.77, 10.0]

不要使用列表作为变量名称

这篇关于在Python中将字符串列表转换为int(或双精度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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