我一直在努力解决这个问题。我有这个列表,我需要转换为一个浮点数,所以我可以在numpy.corrceof()
中使用它和另一个相同的列表。
列表r
如下:
>>> print r
[-0.6680161943319838, -0.7236994219653179, -0.7088915956151035, -0.7388949079089924, -0.7149712092130518, -0.6123110151187905, -0.39853300733496333, -0.017366136034732287, -0.1457800511508952, 0.03546099290780142, 0.0319573901464714, 0.3491027732463295, 0.03203342618384407, 0.2081005586592179, 0.014833127317676267, 0.32045779685264664, 0.31069182389937106, -0.06653225806451614, -0.15583075335397314, -0.2147727272727273, 0.03030303030303036, -0.10076530612244898, -0.2888257575757576, 0.1227106227106227, 0.7095238095238093, 2.808510638297873, 3.7588235294117647, 3.4240506329113924, 4.264900662251656, 5.1234567901234565, 6.34090909090909, 5.666666666666667, 4.163265306122449, 3.611764705882353, 5.439024390243903, 1.9197860962566844, 1.1649484536082475, -0.04081632653061228, -0.17874396135265697, -0.20999999999999996, -0.277511961722488, -0.20197044334975356, -0.2189349112426035, 0.2222222222222222, 0.4134615384615384, 0.47826086956521746, 0.23308270676691714, 0.6120689655172415, 0.9795918367346939, 1.0851063829787235, 1.0294117647058822, 0.6666666666666667, 1.1546391752577319, 1.2065217391304346, 0.69, 0.10204081632653071, -0.009523809523809532, -0.16666666666666666, -0.14193548387096772, -0.24675324675324684, -0.16949152542372878, -0.276923076923077, 0.020000000000000018, 0.7142857142857143, -0.5446009389671361, -0.7401129943502825, -0.7858672376873662, -0.8171641791044777, -0.8, -0.7320388349514564, -0.7327586206896552, -0.7555555555555555, -0.8284883720930233, -0.7833333333333333, -0.8144712430426716, -0.8809523809523809, -0.6120218579234973, -0.33831775700934574, -0.2203672787979967, -0.08688245315161836, -0.19230769230769232, 0.030000000000000072, 0.13725490196078435, 0.24752475247524752, 0.4012219959266802, 0.22448979591836726, 0.19777777777777772, 0.3213483146067415, 0.30714285714285716, 0.5735294117647058, 0.8151515151515153, 0.834375, 1.3636363636363635, 0.7301038062283737, 0.5692307692307691, 0.3015463917525773, 0.6098360655737707, 0.4202898550724638, 0.0975609756097562, 0.6360294117647058, 0.9090909090909091, 0.36, 0.5865384615384615, 0.787709497206704, 1.5943396226415094, 1.7264150943396226, 2.25, 2.88, 1.9901960784313724]
>>> type(r)
<type 'str'>
>>> print len(r)
2169
当我尝试执行float(r)
时:
>>> float(r)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: [-0.6680161943319838, -0.7236994219653179, -0.7088915956151035, -0.7388949079089924, -0.7149712092130518, -0.6123110151187905, -0.39853300733496333, -0.017366136034732287, -0.1457800511508952, 0.03546
从以前的问题中,我看到空值""
可能会导致问题。但是我不知道为什么列表中会有一个空值。即便如此,我也尝试过其他问题的大量方法,例如将list
转换为numpy.array
,然后使用s = r[r=='']='0'
。但是仍然不起作用。
任何人都有任何提示吗?
为什么len(r)
== 2169?
我为构建列表而运行的代码只生成121
个值的列表。如果你真的计算了这个列表,那里有121个值......任何人都可以解释一下吗?
答案 0 :(得分:2)
该字符串是一个表示浮动列表的JSON字符串。
import json
data = json.loads(r)