Python 如何将字符串数字转换为列表?

Python 如何将字符串数字转换为列表?,python,Python,我有一串这样的数字 i = '584,569.2,11515,632' 想把它转换成这样的数字列表 [584,569.2,11515,632] 您可以这样做: i = '584,569.2,11515,632' numbers = list(map(float, i.split(','))) print(numbers) 输出: [584.0, 569.2, 11515.0, 632.0] [584, 569.2, 11515, 632] 此外,作为Chris A,如果int和floa

我有一串这样的数字

i = '584,569.2,11515,632'
想把它转换成这样的数字列表

[584,569.2,11515,632]

您可以这样做:

i = '584,569.2,11515,632'
numbers = list(map(float, i.split(',')))
print(numbers)
输出:

[584.0, 569.2, 11515.0, 632.0]
[584, 569.2, 11515, 632]
此外,作为Chris A,如果
int
float
之间的区别很重要,您可以使用:

输出:

[584.0, 569.2, 11515.0, 632.0]
[584, 569.2, 11515, 632]

list(map(float,i.split(',))
[float(n)表示i.split(',')中的n]
应该可以解决这个问题。太棒了!祝你好运,如果你陷入困境,问一个你已经尝试和研究过的问题。
[int(x)if x.is_integer(),否则x代表map中的x(float,i.split(',)]
if
int
vs
float
数据类型很重要