我有一个类似于[1.124, 5.152, 6.235, NaN, 5.124, Infinity]
的数组。我想将其导入[1.124, 5.152, 6.235, 0, 5.124, 0]
。我该怎么做?
答案 0 :(得分:0)
基于NaN和Inf的过滤:
print([float(ele) if ele not in {"NaN","Infinity"} else 0 for ele in arr])
如果你正在使用numpy。
arr = np.genfromtxt("a.txt", dtype=float,delimiter=",")
print(arr)
arr[~np.isfinite(arr)] = 0
print(arr)
[ 1.124 5.152 6.235 nan 5.124 inf]
[ 1.124 5.152 6.235 0. 5.124 0.