📌  相关文章
📜  在Python中将字符串浮点数转换为浮点数列表

📅  最后修改于: 2022-05-13 01:55:49.977000             🧑  作者: Mango

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

有时,在处理数据时,我们可能会处理十进制而不是整数的数字。这是数据科学领域的一般情况。让我们讨论如何解决一个问题,其中我们可能有一个逗号分隔的浮点数,我们需要转换为浮点列表。

方法 #1:使用列表理解 + split() + float()
上述方法的组合可用于执行此任务。在此,我们使用 split 将字符串转换为字符串列表,然后使用 float() 将字符串转换为浮点数。

Python3
# Python3 code to demonstrate working of
# Convert String float to float list
# using list comprehension + split() + float()
 
# initializing string
test_str = "3.44, 7.8, 9.12, 100.2, 6.50"
 
# printing original string
print("The original string is : " + test_str)
 
# Convert String float to float list
# using list comprehension + split() + float()
res = [float(idx) for idx in test_str.split(', ')]
 
# printing result
print("The float list is : " + str(res))


Python3
# Python3 code to demonstrate working of
# Convert String float to float list
# using map() + split() + float()
 
# initializing string
test_str = "3.44, 7.8, 9.12, 100.2, 6.50"
 
# printing original string
print("The original string is : " + test_str)
 
# Convert String float to float list
# using map() + split() + float()
res = list(map(float, test_str.split(', ')))
 
# printing result
print("The float list is : " + str(res))


输出 :
The original string is : 3.44, 7.8, 9.12, 100.2, 6.50
The float list is : [3.44, 7.8, 9.12, 100.2, 6.5]

方法 #2:使用 map() + split() + float()
上述功能的组合也可以用来解决这个问题。在此,我们使用 map() 执行将逻辑扩展到整个列表的任务,其余所有功能都按上述方法执行。

Python3

# Python3 code to demonstrate working of
# Convert String float to float list
# using map() + split() + float()
 
# initializing string
test_str = "3.44, 7.8, 9.12, 100.2, 6.50"
 
# printing original string
print("The original string is : " + test_str)
 
# Convert String float to float list
# using map() + split() + float()
res = list(map(float, test_str.split(', ')))
 
# printing result
print("The float list is : " + str(res))
输出 :
The original string is : 3.44, 7.8, 9.12, 100.2, 6.50
The float list is : [3.44, 7.8, 9.12, 100.2, 6.5]