2013-07-19 66 views
3

我想格式化numpy的陣列,並將其保存在一個* .txt文件格式化numpy的陣列,並保存到* .TXT

的numpy的陣列看起來像這樣:

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() 

你認爲那可以嗎?

回答

4

相當簡單:

import numpy as np 

a = np.array([[0.1, 0.2, 0.3, 0.4], [1.1, 1.2, 1.3, 1.4], [2.1, 2.2, 2.3, 2.4]]) 

with open("array.txt", 'w') as h: 
    for row in a: 
     h.write("0") 
     for n, col in enumerate(row): 
      h.write("\t{0}:{1}".format(n+1, col)) # you can change the \t (tab) character to a number of spaces, if that's what you require 
     h.write("\n") 

和輸出:

0  1:0.1 2:0.2 3:0.3 4:0.4 
0  1:1.1 2:1.2 3:1.3 4:1.4 
0  1:2.1 2:2.2 3:2.3 4:2.4 

我原來的例子涉及到大量的磁盤寫操作。如果你的數組很大,這可能非常低效。寫入次數可以減少,但是,如:

with open("array.txt", 'w') as h: 
    for row in a: 
     row_str = "0" 
     for n, col in enumerate(row): 
      row_str = "\t".join([row_str, "{0}:{1}".format(n+1, col)]) 
     h.write(''.join([row_str, '\n'])) 

您可以通過構建一個大的字符串,並在末尾寫這進一步降低寫入的數量只有一個,但在情況下,此將是真正有益的(即一個巨大的數組),然後你從構建一個巨大的字符串到內存問題。無論如何,這取決於你。