如何将看起来像列表的字符串转换为浮点数列表?

ATi*_*our -1 string list python-3.x

我有这个清单:

s = '[ 0.00889175 -0.04808848  0.06218296 0.06312469 -0.00700571\n -0.08287739]'
Run Code Online (Sandbox Code Playgroud)

它包含一个'\n'字符,我想将其转换为这样的列表float

l = [0.00889175, -0.04808848, 0.06218296, 0.06312469, -0.00700571, -0.08287739]
Run Code Online (Sandbox Code Playgroud)

我尝试了这段代码,它接近我想要的代码:

l = [x.replace('\n','').strip(' []') for x in s.split(',')]
Run Code Online (Sandbox Code Playgroud)

但是它仍然保留我没有设法删除的引号(我尝试过str.replace("'","")但没有用),这就是我得到的:

['0.00889175 -0.04808848  0.06218296 0.06312469 -0.00700571 -0.08287739']
Run Code Online (Sandbox Code Playgroud)

ruo*_*ola 6

你很亲近 这将起作用:

s = '[ 0.00889175 -0.04808848  0.06218296 0.06312469 -0.00700571\n -0.08287739]'

l = [float(n) for n in s.strip("[]").split()]

print(l)
Run Code Online (Sandbox Code Playgroud)

输出:

s = '[ 0.00889175 -0.04808848  0.06218296 0.06312469 -0.00700571\n -0.08287739]'

l = [float(n) for n in s.strip("[]").split()]

print(l)
Run Code Online (Sandbox Code Playgroud)