a = [ 0.1 0.2 0.3 0.4 ... ] , [ 1.1 1.2 1.3 1.4 ... ] , ...
输出的*.txt文件应该是这样的:
0 1:0.1 2:0.2 3:0.3 4:0.4 ...
0 1:1.1 2:1.2 3:1.3 1:1.4 ...
...
不知道如何做。
谢谢。
嗯,Jaba,谢谢你。我稍微修改了你的答案。
import numpy as np
a = np.array([[1,3,5,6], [4,2,4,6], [6,3,2,6]])
ret = ""
for i in range(a.shape[0]):
ret += "0 "
for j in range(a.shape[1]):
ret += " %s:%s" % (j+1,float(a[i,j])) #have a space between the numbers for better reading and i think it should starts with 1 not with 0 ?!
ret +="\n"
fd = open("output.sparse", "w")
fd.write(ret)
fd.close()
你觉得这样行吗?!