gpt4 book ai didi

python - 尝试绘制两行数据时出现 "Could not Convert String to Float"错误

转载 作者:行者123 更新时间:2023-12-01 03:05:47 26 4
gpt4 key购买 nike

我正在尝试绘制两行数据,但我在这样做时遇到了困难。由于某种原因,在替换了我认为导致问题的所有内容后,我仍然遇到了错误。我尝试了不同的方法,例如 .replace().split(),但它们没有帮助。我的代码如下:

#import csv
#import numpy as np
#import string
#import pandas as pd
from matplotlib import pyplot as plt

def split_list(a_list):
half = len(a_list)/2
return a_list[:half], a_list[half:]

c = []
B1 = []
C1 = []

a = [0, 1]

with open('Lamda_HeHCL_T.txt') as fd:
for n, line in enumerate(fd):
if n in a:
c.append(line.strip())

B, C = split_list(c)

B = str(B)
C = str(C)

B = B.replace("'", "").replace("\n","")
B = B.replace(" ", ",")
B = B.replace(" ", "")
B = B.replace(",", " ")

C = C.replace("'","").replace("\n","")
C = C.replace(" ", ",")
C = C.replace("1,,,,","")
C = C.replace("2,,,","")

B = B.strip('[]')
print B
B = map(float, B)

print B
print C

#fix, ax = plt.subplots()
#ax.scatter(B, C)

这是我正在使用的数据:

 1      2        5      10      20      30      40      50      60      70      80       90      100      150      200      300      400      500      600      700      800      900     1000     1500     2000     2500     3000
1 2 1 4.151E-12 4.553E-12 4.600E-12 4.852E-12 6.173E-12 7.756E-12 9.383E-12 1.096E-11 1.243E-11 1.379E-11 1.504E-11 1.619E-11 1.724E-11 2.139E-11 2.426E-11 2.791E-11 3.009E-11 3.152E-11 3.252E-11 3.326E-11 3.382E-11 3.426E-11 3.462E-11 3.572E-11 3.640E-11 3.698E-11 3.752E-11

我注释掉了绘图代码,直到我能够成功地将它们转换为 float ,以便可以绘制它们。

完整回溯:

Traceback (most recent call last):

File "<ipython-input-371-b1de68ec731e>", line 1, in <module>
runfile('C:/Users/.spyder/Finding_C_New_New.py', wdir='C:/Users/.spyder')

File "C:\Program Files\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)

File "C:\Program Files\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/Users/.spyder/Finding_C_New_New.py", line 49, in <module>
B = map(float, B)

ValueError: could not convert string to float:

最佳答案

您的问题是 BC 都是列表的字符串表示形式。解决方案是通过简单的列表理解将 BC 转换为列表,并使用 ast 模块:

原始代码

#import csv
#import numpy as np
#import string
#import pandas as pd
from matplotlib import pyplot as plt

def split_list(a_list):
half = len(a_list)/2
return a_list[:half], a_list[half:]

c = []
B1 = []
C1 = []

a = [0, 1]

with open('Lamda_HeHCL_T.txt') as fd:
for n, line in enumerate(fd):
if n in a:
c.append(line.strip())

B, C = split_list(c)

B = str(B)
C = str(C)

B = B.replace("'", "").replace("\n","")
B = B.replace(" ", ",")
B = B.replace(" ", "")
B = B.replace(",", " ")

C = C.replace("'","").replace("\n","")
C = C.replace(" ", ",")
C = C.replace("1,,,,","")
C = C.replace("2,,,","")

B = B.strip('[]')
print B

此时,我们看到B是一个字符串:

B = "1 2 5 10 20 30 40 50 60 70 80 90 100 150 200 300 400 500 600 700 800 900 1000 1500 2000 2500 3000"

C 是列表的字符串表示形式:

C = '[4.151E-12,4.553E-12,4.600E-12,4.852E-12,6.173E-12,7.756E-12,9.383E-12,1.096E-11,1.243E-11,1.379E-11,1.504E-11,1.619E-11,1.724E-11,2.139E-11,2.426E-11,2.791E-11,3.009E-11,3.152E-11,3.252E-11,3.326E-11,3.382E-11,3.426E-11,3.462E-11,3.572E-11,3.640E-11,3.698E-11,3.752E-11]'

您需要通过执行以下操作将它们更改为列表:

# For B
B = [float(i) for i in B.split()]
>>> B =
[1.0, 2.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 150.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0, 900.0, 1000.0, 1500.0, 2000.0, 2500.0,3000.0]

# For C
import ast
C = ast.literal_eval(C)
> C =
[4.151e-12, 4.553e-12, 4.6e-12, 4.852e-12, 6.173e-12, 7.756e-12, 9.383e-12, 1.096e-11, 1.243e-11, 1.379e-11, 1.504e-11, 1.619e-11, 1.724e-11, 2.139e-11, 2.426e-11, 2.791e-11, 3.009e-11, 3.152e-11, 3.252e-11, 3.326e-11, 3.382e-11, 3.426e-11, 3.462e-11, 3.572e-11, 3.64e-11, 3.698e-11, 3.752e-11]

然后像以前一样继续:

B = map(float, B)    
fix, ax = plt.subplots()
ax.scatter(B, C)

给你:

Scatter of C vs B

关于python - 尝试绘制两行数据时出现 "Could not Convert String to Float"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43438691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com