我有很多与此类似的字符串列表:
list = ['4', '-5', '5.763', '6.423', '-5', '-6.77', '10']
我想将其转换为整数(或双精度)列表,但-
不断产生错误。
>>> 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
用作变量名
对于 Python 3:
listOfStrings = ['4', '-5', '5.763', '6.423', '-5', '-6.77', '10']
listOfFloats = list(map(float, listOfStrings))
>>> [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]