如何在python中将列表类型转换为浮点数

萨姆拉德尼

我正在尝试将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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Python中将numpy转换为浮点数

如何在python数据框中将对象数据类型转换为浮点数

如何在JavaScript中将浮点数转换为整数?

如何在 OpenCL 中将 int 转换为浮点数?

如何在Elixir中将浮点数转换为整数

如何在PHP中将数字转换为浮点数

如何在数据框中将列类型对象转换为浮点数

如何在Julia中将某些计算值(浮点数)转换为RGB类型?

如何在Python 2.7中将字符串转换为浮点数

如何在python中将十六进制值转换为浮点数

如何在Python中将带点和逗号的字符串转换为浮点数

如何在Python中将带分数的字符串转换为浮点数

在python中将浮点数转换为浮点时间

在Python中将字符串转换为浮点数

在Python中将数字转换为浮点数

在python中将固定点转换为浮点数的问题

如何在python3中将浮点数和字符串转换为整数值

ValueError: 无法将字符串转换为浮点数---如何将字符串列表的列表转换为 numpy 数组类型浮点数?

如何在Rails中将字符串(“€289,95”)转换为浮点数(“ 289.95”)?

如何在Julia v1.0.3中将浮点数转换为Int

如何在Java中将浮点数组转换为双精度数组?

如何在csv文件中将字符串转换为浮点数

如何在Perl中将浮点数转换为十六进制字符串?

如何在 Node.js 中将 4 字节十六进制转换为浮点数

MemSQL - 如何在 MemSQL 中将整数/十进制值转换为浮点数?

如何在Crystal中将字符串转换为整数或浮点数?

如何在javascript中将stringyfied数组值转换为浮点数

如何在 PHP 中将长浮点数转换为字符串?

如何在 C# 中将整数 999999999 转换为浮点数 999999999.0 而不是 1000000000.0?