Python 格式化numpy数组并保存为*.txt

Python 格式化numpy数组并保存为*.txt,python,arrays,numpy,formatting,Python,Arrays,Numpy,Formatting,我想格式化numpy数组并将其保存在*.txt文件中 numpy数组如下所示: a = [ 0.1 0.2 0.3 0.4 ... ] , [ 1.1 1.2 1.3 1.4 ... ] , ... 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 ... ... 输出*.txt应如下所示: a = [ 0.1 0.2 0.3 0.4 ...

我想格式化numpy数组并将其保存在*.txt文件中

numpy数组如下所示:

a = [ 0.1   0.2   0.3   0.4   ... ] , [ 1.1   1.2   1.3   1.4   ... ] , ...
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   ...
...
输出*.txt应如下所示:

a = [ 0.1   0.2   0.3   0.4   ... ] , [ 1.1   1.2   1.3   1.4   ... ] , ...
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   ...
...
我不知道怎么做

多谢各位

谢谢你。我修正了你的答案

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()
你觉得这样行吗

相当简单:

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']))
通过构造一个大字符串并在最后写入,可以将写入次数进一步减少到仅一次,但如果这确实是有益的(即,一个巨大的数组),那么构造一个巨大的字符串会导致内存问题。不管怎样,这取决于你