gpt4 book ai didi

python - 解决错误: could not convert string to float: '0.1222000000*10e1'

转载 作者:行者123 更新时间:2023-12-01 04:44:08 28 4
gpt4 key购买 nike

expo2 = ['0.1222000000*10E1', '0.9310000000*10E-1', '0.2580000000*10E1', '0.2580000000*10E1', '0.2580000000*10E1', '0.8850000000*10E-1', '0.8850000000*10E-1', '0.8850000000*10E-1', '0.8850000000*10E0', '0.8850000000*10E0', '0.8850000000*10E0', '0.8850000000*10E0', '0.8850000000*10E0', '0.1222000000*10E1', '0.9310000000*10E-1', '0.2580000000*10E1', '0.2580000000*10E1', '0.2580000000*10E1', '0.8850000000*10E-1', '0.8850000000*10E-1', '0.8850000000*10E-1', '0.8850000000*10E0', '0.8850000000*10e0', '0.8850000000*10E0', '0.8850000000*10E0', '0.8850000000*10E0']    

我有一个数字列表。我想使用它们,所以我尝试使用 float 命令,但是,当我写道:

print(float(expo2[0]))

我收到此错误消息:

ValueError: could not convert string to float: '0.1222000000*10e1'

如何将这些数字转换为 float ?

最佳答案

我认为这实际上是正确的公式:

expo2 = ['0.1222000000*10e1', '0.9310000000*10e-1', '0.2580000000*10e1', '0.2580000000*10e1', '0.2580000000*10e1', '0.8850000000*10e-1', '0.8850000000*10e-1', '0.8850000000*10e-1', '0.8850000000*10e0', '0.8850000000*10e0', '0.8850000000*10e0', '0.8850000000*10e0', '0.8850000000*10e0', '0.1222000000*10e1', '0.9310000000*10e-1', '0.2580000000*10e1', '0.2580000000*10e1', '0.2580000000*10e1', '0.8850000000*10e-1', '0.8850000000*10e-1', '0.8850000000*10e-1', '0.8850000000*10e0', '0.8850000000*10e0', '0.8850000000*10e0', '0.8850000000*10e0', '0.8850000000*10e0']


floats = []
for f in expo2:
try:
a, e = f.split("*")
floats.append(float(a) * float(e))
except ValueError as e:
print("Not a float")
print(floats)

[12.22, 0.931, 25.8, 25.8, 25.8, 0.885, 0.885, 0.885, 8.85, 8.85, 8.85, 8.85, 8.85, 12.22, 0.931, 25.8, 25.8, 25.8, 0.885, 0.885, 0.885, 8.85, 8.85, 8.85, 8.85, 8.85]

您可以使用 eval 进行验证:

print(all(a == eval(b) for a,b in zip(floats, expo2)))
True

10e-1 为 1 10e1 为 100:

In [25]: 10e-1
Out[25]: 1.0
In [26]: 10e-2
Out[26]: 0.1
In [27]: 10e-3
Out[27]: 0.01
In [28]: 10e1 # 10 * 10^1 -> 10 * 10
Out[28]: 100.0
In [29]: 10e2 # 10 * 10^2 -> 10 * 10 * 10
Out[29]: 1000.0
In [30]: 10e3 # 10 * 10^3 -> 10 * 10 * 10 * 10
Out[30]: 10000.0

来自this site的表格使用1.0作为系数:

1.0 is Coefficient

10 is BASE

N is EXPONENT

1.0*10E-24= 0.000,000,000,000,000,000,000,001 or Yocto

1.0*10E-21= 0.000,000,000,000,000,000,001 or Zepto

1.0*10E-18= 0.000,000,000,000,000,001 or 1 Quintillionth, alto

1.0*10E-15= 0.000,000,000,000,001 or 1 quadrillionth, femto

1.0*10E-12= 0.000,000,000,001 or 1 Trillionth, Pico

1.0*10E-11= 0.000,000,000,01

1.0*10E-10= 0.000,000,000,1

1.0*10E-09= 0.000,000,001 or 1 billionth, nano

1.0*10E-08= 0.000,000,01

1.0*10E-07= 0.000,000,1

1.0*10E-06= 0.000,001 or 1 Millionth, micro

1.0*10E-05= 0.000,01

1.0*10E-04= 0.000,1

1.0*10E-03= 0.001 or 1 thousandth, milli

1.0*10E-02= 0.01 or centi

1.0*10E-01= 0.1 or deci

1.0*10E00= 1 or Units or (V,I,R,P,)(Meter,Liter,Gram,)(Seconds)ƒ(Herz Cycles)

1.0*10E01= 10 or Deka

1.0*10E02= 100 or Hecto

1.0*10E03= 1,000 or Kilo

1.0*10E04= 10,000 or (10K)

1.0*10E05= 100,000 or (100K)

1.0*10E06= 1,000,000 or Mega

1.0*10E07= 10,000,000 or (10M)

1.0*10E08= 100,000,000 or (100M)

1.0*10E09= 1,000,000,000 or Giga

1.0*10E12= 100,000 000,000 or Tera

1.0*10E15= 100,000,000,000,000 or Peta

1.0*10E18= 100,000,000,000,000,000 or Exa

1.0*10E21= 100,000,000,000,000,000,000 or Zetta

1.0*10E24= 100,000,000,000,000,000,000,000 or Yotta

如果你有混合物:

expo2 = ['0.1222000000*10e2', "1.0", "4r", "2.123e4", '0.9310000000*10e-1', '0.2580000000*10e1',"foo"]

floats = []


for f in expo2:
try:
a, e = f.split("*")
floats.append(float(a) * float(e))
except ValueError:
try:
floats.append(float(f))
except ValueError as e:
print(e)

could not convert string to float: '4r'
could not convert string to float: 'foo'
[122.2, 1.0, 21230.0, 0.931, 25.8]

关于python - 解决错误: could not convert string to float: '0.1222000000*10e1' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29862693/

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