我正在尝试将list
数据类型转换为float
. 我试过以下代码但它不起作用,listf
仍然将数据类型显示为列表
for x in range(0,5):
timeVar.append((matchedLine4[x]).replace(stringToMatch4,'').rstrip())
listf=map(float,timeVar)
#list1 = [float(i) for i in timeVar]
print(type(listf))
您通常不能将列表转换为浮点数。但是,如果您先将字符串转换为浮点数然后添加到列表中,则会更容易,并且可能如下所示:
matchedLine4 = '1 2 3x 4 5.7'.split()
listf = []
stringToMatch4 = 'x'
for x in range(0, 5):
timeVar = float(matchedLine4[x].replace(stringToMatch4, '').rstrip())
listf.append(timeVar)
print(listf)
[1.0, 2.0, 3.0, 4.0, 5.7]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句