将逗号分隔的浮点数字符串转换为列表? [英] Convert comma separated string of floats into list?
本文介绍了将逗号分隔的浮点数字符串转换为列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要定义一个函数 txtnum(L)
,该函数将以逗号分隔的浮点字符串(例如"1.5,2.5,3.5"
)作为参数并将其转换进入列表 [1.5,2.5,3.5]
.
I need to define a function txtnum(L)
that takes a string of comma separated floats such as "1.5,2.5,3.5"
as a parameter and converts it into a list [1.5, 2.5, 3.5]
.
我尝试使用 .split()
, .join()
, map()
等,但无法获取任何内容来返回不包含报价的列表.我是Python的新手,在这里有些迷路.
I have tried using .split()
, .join()
, map()
, etc and cannot get anything to return a list that does NOT include quotations. I'm pretty new to Python and a little lost here.
我该怎么做?
推荐答案
您需要转换已拆分var的数据类型,因为单独拆分字符串会为您提供字符串列表.
You need to convert the datatype of splitted vars because splitting alone string gives you a list of strings.
>>> s = "1.5,2.5,3.5"
>>> [float(i) for i in s.split(',')]
[1.5, 2.5, 3.5]
>>>
或
>>> map(float, s.split(','))
[1.5, 2.5, 3.5]
这篇关于将逗号分隔的浮点数字符串转换为列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文