当项目包含括号时,将大字符串转换为浮点列表

时间:2015-05-27 16:55:40

标签: python

我有一个包含字符串的变量r,但是我需要它作为浮点数列表。这个论坛已被多次覆盖,但我似乎无法找到符合此案例的那个:

>>> print r
[0.06481481481481466, 0.06481481481481466, 0.06481481481481466, 0.0754716981132074, 0.07619047619047604, 0.07619047619047604, 0.07692307692307698, 0.07766990291262142, 0.07766990291262142, 0.07843137254901968, 0.07920792079207928, 0.07920792079207928, 0.08000000000000007, 0.08000000000000007]
>>> type(r)
<type 'str'>

当我尝试拆分它时......

    >>> r.split(',')
['[0.06481481481481466', ' 0.06481481481481466', ' 0.06481481481481466', ' 0.0754716981132074', ' 0.07619047619047604', ' 0.07619047619047604', ' 0.07692307692307698', ' 0.07766990291262142', ' 0.07766990291262142', ' 0.07843137254901968', ' 0.07920792079207928', ' 0.07920792079207928', ' 0.08000000000000007', ' 0.08000000000000007]']

现在,r中的第一个和最后一个项目分别包含'['']'。很明显,为了将其转换为浮点数,我需要删除它们。

这是我已经做到的,但它似乎不起作用......

def stringlist_to_floatlist(e):
    split = e.split(',')
    l = len(split)
    for i in split:
        for br in ["[","]"]:
            if br in i:
                try:
                    i = float(i.replace(br,""))
                except TypeError:
                    continue
            else:
                try:
                    i = float(i)
                except TypeError:
                    continue
    return i 


>>> stringlist_to_floatlist(r)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in stringlist_to_floatlist
TypeError: argument of type 'float' is not iterable

3 个答案:

答案 0 :(得分:3)

>>> ast.literal_eval('[0.06481481481481466, 0.06481481481481466, 0.06481481481481466, 0.0754716981132074, 0.07619047619047604, 0.07619047619047604, 0.07692307692307698, 0.07766990291262142, 0.07766990291262142, 0.07843137254901968, 0.07920792079207928, 0.07920792079207928, 0.08000000000000007, 0.08000000000000007]')
[0.06481481481481466, 0.06481481481481466, 0.06481481481481466, 0.0754716981132074, 0.07619047619047604, 0.07619047619047604, 0.07692307692307698, 0.07766990291262142, 0.07766990291262142, 0.07843137254901968, 0.07920792079207928, 0.07920792079207928, 0.08000000000000007, 0.08000000000000007]
>>> ast.literal_eval('[0.06481481481481466, 0.06481481481481466, 0.06481481481481466, 0.0754716981132074, 0.07619047619047604, 0.07619047619047604, 0.07692307692307698, 0.07766990291262142, 0.07766990291262142, 0.07843137254901968, 0.07920792079207928, 0.07920792079207928, 0.08000000000000007, 0.08000000000000007]')[0]
0.06481481481481466

答案 1 :(得分:1)

此处的其他答案效果很好,但由于您似乎从外部源获取此字符串,因此您可能正在解析json格式化的字符串。 Python有一个内置的json解析器,所以你可以这样做:

>>> import json
>>> r = """[0.06481481481481466, 0.06481481481481466, 0.06481481481481466, 0.0754716981132074, 0.07619047619047604, 0.07619047619047604, 0.07692307692307698, 0.07766990291262142, 0.07766990291262142, 0.07843137254901968, 0.07920792079207928, 0.07920792079207928, 0.08000000000000007, 0.08000000000000007]"""
>>> json.loads(r)
[0.06481481481481466, 0.06481481481481466, 0.06481481481481466, 0.0754716981132074, 0.07619047619047604, 0.07619047619047604, 0.07692307692307698, 0.07766990291262142, 0.07766990291262142, 0.07843137254901968, 0.07920792079207928, 0.07920792079207928, 0.08000000000000007, 0.08000000000000007]
>>> type(json.loads(r))
list

答案 2 :(得分:1)

尽管python具有用于解析json和其他格式的特定工具,但是如果你有一个字符串通常被你想要摆脱的开头和结尾的某些字符括起来,你可以用{{1来指定那些字符string builtin:

strip