2 回答
TA贡献1810条经验 获得超4个赞
如果这些是numpy数组,您可以使用np.savetxtwith delimiter='\n',并枚举您的数组:
给定数组x:
>>> x
array([[-3.75536609e+00, 9.41899478e-01, 1.59805989e+00,
-8.97728577e-02, -3.67324662e+00, 2.85971731e-01],
[-1.48593640e+00, 6.56555116e-01, 6.41966641e-01,
-2.62360811e-01, -4.66498184e+00, -8.62938017e-02],
[ 7.05197873e-03, -4.12116805e-03, -1.30933505e-02,
5.76611329e-03, -6.51661423e-04, -9.73143987e-03]])
用:
[np.savetxt(f'array{n}.txt',i,delimiter='\n') for n,i in enumerate(x)]
你会得到 3 个文本文件:
array0.txt 将:
-3.755366089999999879e+00
9.418994779999999567e-01
1.598059890000000038e+00
-8.977285769999999998e-02
-3.673246620000000018e+00
2.859717310000000068e-01
等等...
[编辑]看来我误解了你的问题。如果您只想要一个 txt 文件,请使用:
np.savetxt('array.txt', x.flatten(), delimiter='\n')
这将给出array.txt:
-3.755366089999999879e+00
9.418994779999999567e-01
1.598059890000000038e+00
-8.977285769999999998e-02
-3.673246620000000018e+00
2.859717310000000068e-01
-1.485936399999999935e+00
6.565551160000000497e-01
6.419666410000000045e-01
-2.623608110000000271e-01
-4.664981840000000268e+00
-8.629380170000000572e-02
7.051978730000000098e-03
-4.121168050000000374e-03
-1.309335049999999986e-02
5.766113290000000370e-03
-6.516614230000000382e-04
-9.731439870000000575e-03
添加回答
举报