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'我怎样才能把这些数字转换成浮动?
发布于 2015-04-25 10:46:48
我认为这其实是正确的公式:
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)))
True10e-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使用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]发布于 2015-04-25 09:44:36
列表中的数字格式与Python的float()函数使用的格式不匹配。您应该对字符串进行处理,以实际获得它们的浮点数。您的问题的解决方案可能是拆分字符串以表示两个浮点数,然后将它们相乘,如下所示:
from sys import stderr
callback = lambda x: stderr.write('Invalid floating point number found:{0}'.format(x))
def convert(src, error_callback):
ret = list()
for item in src:
index = item.find('*')
if index == -1:
error_callback(item)
continue
try: ret.append(float(item[:index]) * (float(item[index+1:])/10))
except ValueError: error_callback(item)
return ret现在如果你打电话:
expo2_floats = convert(expo2, callback)你得到:
[1.222, 0.09310000000000002, 2.58, 2.58, 2.58, 0.08850000000000001, 0.08850000000000001, 0.08850000000000001, 0.885, 0.885, 0.885, 0.885, 0.885, 1.222, 0.09310000000000002, 2.58, 2.58, 2.58, 0.08850000000000001, 0.08850000000000001, 0.08850000000000001, 0.885, 0.885, 0.885, 0.885, 0.885]发布于 2015-04-25 08:50:53
去掉'标记。通过在这些标记中扭曲数字,r使它们的字符串浮动不起作用!对于字符串,请执行以下操作
expo2=[2125*10e1,.........]
print expo2[0]看起来不错
https://stackoverflow.com/questions/29862693
复制相似问题