在Python字符串转换的列表,以整数(或双打)(Converting a list of stri

2019-09-20 11:23发布

我有很多的字符串,类似于这样的名单:

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

我想将其转换为整数(或双打)的列表,但-不断产生错误。

Answer 1:

>>> 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]

并且不使用list作为变量名



Answer 2:

>>> [float(x) for x in ['4', '-5', '5.763', '6.423', '-5', '-6.77', '10']]
[4.0, -5.0, 5.763, 6.423, -5.0, -6.77, 10.0]


Answer 3:

为Python 3:

listOfStrings = ['4', '-5', '5.763', '6.423', '-5', '-6.77', '10']
listOfFloats = list(map(float, listOfStrings))


文章来源: Converting a list of strings to ints (or doubles) in Python