TypeError:需要浮点数

时间:2017-08-27 15:26:03

标签: python python-2.7 python-3.x

您好,我有这段代码:

import math
import numpy as np
a = np.array([2.79e-06,2.47e-06,0.000122,3.9e-05,0.000527,9.41e-06,3.11e-05,4.63e-05,0.000749,0.000499,0.000174,0.000984,0.000912,5.86e-05,0.000344,0.000754,0.000267,2.53e-06,8.73e-05,0.000791,1.69e-06,7.56e-05,0.001,3.1e-06,0.000305,2.4e-06,6.37e-06,3.25e-07,6.67e-05,0.000167,0.000954,2.36e-07,3.8e-06,0.000337,8.1e-06,1.68e-05,0.000332,3.64e-06,"0,00002",5.97e-06,0.000808,2.34e-06,0.000121,0.000972,2.59e-05,0.000761,8.76e-05,0.000253,0.000819,7.88e-06,5.04e-05,1.75e-05,5.83e-05,0.000271,3.18e-06,3.29e-05,0.000979,0.000925,2.55e-05,0.000347,0.000269,9.51e-06,5.54e-06,7.18e-05,1.44e-05,8.42e-09,1.86e-05,0.000377,1.68e-05,0.000991,4.69e-06,9.87e-05,4.45e-05,4.05e-06,6.76e-05,5.66e-06,6.51e-06,3.76e-06,6.44e-05,2.91e-09,0.000565,9.18e-06,0.0003,0.0002,9.43e-05,8.57e-06,6.32e-05,"0,000004",5.18e-06,0.000181,0.000999,1.67e-05,0.000941,6.49e-05,0.000141,4.07e-06,2.68e-06,0.000407,1.47e-05,5.2e-06,6.53e-06,0.000462,1.38e-05,0.000794,3.32e-07,2.19e-06,0.000432,0.000156
])
b = np.array([math.log10(i) for i in a])

我不知道为什么但是我收到了这个错误:TypeError: a float is require

我该如何解决这个问题?

谢谢!

2 个答案:

答案 0 :(得分:1)

a包含"0,000004"字符串。

您应该删除传递给np.array的列表中的所有字符串。

要识别包含不浮动值的列表的所有索引和值,您可以执行以下操作:

l = [2.79e-06,2.47e-06,0.000122,3.9e-05,0.000527,9.41e-06,3.11e-05,4.63e-05,0.000749,0.000499,0.000174,0.000984,0.000912,5.86e-05,0.000344,0.000754,0.000267,2.53e-06,8.73e-05,0.000791,1.69e-06,7.56e-05,0.001,3.1e-06,0.000305,2.4e-06,6.37e-06,3.25e-07,6.67e-05,0.000167,0.000954,2.36e-07,3.8e-06,0.000337,8.1e-06,1.68e-05,0.000332,3.64e-06,"0,00002",5.97e-06,0.000808,2.34e-06,0.000121,0.000972,2.59e-05,0.000761,8.76e-05,0.000253,0.000819,7.88e-06,5.04e-05,1.75e-05,5.83e-05,0.000271,3.18e-06,3.29e-05,0.000979,0.000925,2.55e-05,0.000347,0.000269,9.51e-06,5.54e-06,7.18e-05,1.44e-05,8.42e-09,1.86e-05,0.000377,1.68e-05,0.000991,4.69e-06,9.87e-05,4.45e-05,4.05e-06,6.76e-05,5.66e-06,6.51e-06,3.76e-06,6.44e-05,2.91e-09,0.000565,9.18e-06,0.0003,0.0002,9.43e-05,8.57e-06,6.32e-05,"0,000004",5.18e-06,0.000181,0.000999,1.67e-05,0.000941,6.49e-05,0.000141,4.07e-06,2.68e-06,0.000407,1.47e-05,5.2e-06,6.53e-06,0.000462,1.38e-05,0.000794,3.32e-07,2.19e-06,0.000432,0.000156]
print([(i, v) for i, v in enumerate(l) if type(v) != float])

答案 1 :(得分:0)

错误是因为某些元素是字符串形式,小数点用逗号而不是小数点标记。您可以通过将逗号替换为小数点并将类型转换为浮动来解决此问题,如下所示:

import math
import numpy as np
a = np.array([2.79e-06,2.47e-06,0.000122,3.9e-05,0.000527,9.41e-06,3.11e-05,4.63e-05,0.000749,0.000499,0.000174,0.000984,0.000912,5.86e-05,0.000344,0.000754,0.000267,2.53e-06,8.73e-05,0.000791,1.69e-06,7.56e-05,0.001,3.1e-06,0.000305,2.4e-06,6.37e-06,3.25e-07,6.67e-05,0.000167,0.000954,2.36e-07,3.8e-06,0.000337,8.1e-06,1.68e-05,0.000332,3.64e-06,"0,00002",5.97e-06,0.000808,2.34e-06,0.000121,0.000972,2.59e-05,0.000761,8.76e-05,0.000253,0.000819,7.88e-06,5.04e-05,1.75e-05,5.83e-05,0.000271,3.18e-06,3.29e-05,0.000979,0.000925,2.55e-05,0.000347,0.000269,9.51e-06,5.54e-06,7.18e-05,1.44e-05,8.42e-09,1.86e-05,0.000377,1.68e-05,0.000991,4.69e-06,9.87e-05,4.45e-05,4.05e-06,6.76e-05,5.66e-06,6.51e-06,3.76e-06,6.44e-05,2.91e-09,0.000565,9.18e-06,0.0003,0.0002,9.43e-05,8.57e-06,6.32e-05,"0,000004",5.18e-06,0.000181,0.000999,1.67e-05,0.000941,6.49e-05,0.000141,4.07e-06,2.68e-06,0.000407,1.47e-05,5.2e-06,6.53e-06,0.000462,1.38e-05,0.000794,3.32e-07,2.19e-06,0.000432,0.000156
])
b = np.array([math.log10(float(i.replace(",",'.'))) for i in a])
print (b)