简体   繁体   English

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

[英]Convert a large string into a list of floats when an item contains brackets

I have a variable r that contains a string, however I need it to be a list of floats. 我有一个包含字符串的变量r ,但是我需要它成为浮点数列表。 This has been covered multiple times in this forum, however I can't seem to find one that matches this case: 这个论坛已经对此进行了多次讨论,但是我似乎找不到与这种情况匹配的一个:

>>> 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'>

When I try to split it... 当我尝试拆分时...

    >>> 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]']

Now the first and last item in r contain a '[' and ']' respectively. 现在,r中的第一项和最后一项分别包含一个'['']' So obviously, in order to convert this into a float, I need to remove these. 所以很显然,为了将其转换为浮点数,我需要删除它们。

This is what I have gotten up to, but it doesn't seem to work... 这是我要做的,但是似乎不起作用...

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
>>> 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

The other answers here work well, but since it seems you're getting this string from an outside source, it's likely that you're parsing json formatted strings. 此处的其他答案很好用,但是由于您似乎是从外部来源获取此字符串,因此很可能是在解析json格式的字符串。 Python has a built-in json parser, so you can just do: 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

Although python has specific tools for parsing json and other formats, if you have a string that is generally bracketed by some characters at the beginning and end that you want to get rid of, you can specify those characters with the strip string builtin: 尽管python有用于解析json和其他格式的特定工具,但是如果您要删除的字符串通常在开头和结尾处都被某些字符括起来,则可以使用内置的strip字符串指定这些字符:

>>> s = '[4.4,3.2,1.1]'
>>> s.strip('[]')
'4.4,3.2,1.1'
>>> s = '[I am not: a ; valid .,:{json string]'
>>> s.strip('[]')
'I am not: a ; valid .,:{json string'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM