for循环中的2D和1D numpy数组保存到文件中

时间:2018-01-15 11:48:37

标签: python numpy for-loop multidimensional-array slice

问题说明

给出两个2D numpy数组和一个1D numpy数组:

F =  np.array[[-3764.9303402755891, -3764.9303494098212, -3764.9304257856452, -3764.9306329129422], [-3764.9338022566421, -3764.9338129752682, -3764.9338970928361, -3764.9341184863633]]

T =  np.array[[ 10.,   30.1,  50.2,  70.3],
 [ 10.,   30.1,  50.2,  70.3]]

V =  np.array[ 226.331804,  228.817957]

我想生成4个文件:

# F_10.0K.dat:
226.331804   -3764.9303402755891
228.817957   -3764.9338022566421

# F_30.1K.dat:
226.331804    -3764.9303494098212
228.817957    -3764.9338129752682

# F_50.2K.dat:
226.331804    -3764.9304257856452
228.817957    -3764.9338970928361

# F_70.3K.dat:
226.331804     -3764.9306329129422
228.817957     -3764.9341184863633

我的尝试:

我注意到这两个切片操作:

print ' F_all[:,0] = ', F_all[:, 0]
print ' F_all[:,1] = ', F_all[:, 1]

返回:

 F_all[:,0] =  [-3764.93034028 -3764.93380226]
 F_all[:,1] =  [-3764.93034941 -3764.93381298]

这是前两个文件的第2列:F_10.0K.datF_30.1K.dat

所以,我可以循环:

F_all_each_V_at_cte_T = []
for indx in range(0, cols):
   aux = F_all[:,indx]
   print ' F_all[:, indx] = ', F_all[:,indx]
   F_all_each_V_at_cte_T.append(aux)

print 'F_all_each_V_at_cte_T = ', F_all_each_V_at_cte_T

output_array = np.vstack((VOLUME_EACH, F_all_each_V_at_cte_T)).T
np.savetxt('F_vs_V_10.0K.dat', output_array, header="Volume    F at 10.0K", fmt="%0.13f")

结果是:

F_all_each_V_at_cte_T =  [array([-3764.93034028, -3764.93380226]), array([-3764.93034941, -3764.93381298]), array([-3764.93042579, -3764.93389709]), array([-3764.93063291, -3764.93411849])]

# Volume    F at 10.0K
226.3318040000000 -3764.9303402755891 -3764.9303494098212 -3764.9304257856452 -3764.9306329129422
228.8179570000000 -3764.9338022566421 -3764.9338129752682 -3764.9338970928361 -3764.9341184863633

这几乎可以实现解决方案,但是,所有列都已打印出来。如何生成上述文件?

3 个答案:

答案 0 :(得分:1)

With your F and V (I added () to make valid Python statements):

In [147]: F =  np.array([[-3764.9303402755891, -3764.9303494098212, -3764.930425
     ...: 7856452, -3764.9306329129422], [-3764.9338022566421, -3764.93381297526
     ...: 82, -3764.9338970928361, -3764.9341184863633]])
In [148]: V =  np.array([ 226.331804,  228.817957])
In [149]: F.shape
Out[149]: (2, 4)
In [150]: V.shape
Out[150]: (2,)

Since your F values differ on the small decimals it's a little tricky to figure out how you want to pair up values. But this appears to be what you want:

In [152]: for i in range(4):
     ...:     print(np.column_stack((V,F[:,i])))

[[  226.331804   -3764.93034028]
 [  228.817957   -3764.93380226]]
[[  226.331804   -3764.93034941]
 [  228.817957   -3764.93381298]]
[[  226.331804   -3764.93042579]
 [  228.817957   -3764.93389709]]
[[  226.331804   -3764.93063291]
 [  228.817957   -3764.93411849]]

This isn't showing all the decimal values, but that's just a default numpy display issue. I'm using column_stack as a convenient way of concatenating two 1d arrays as columns. I could just as well have used stack or concatenate with the appropriate dimensions adjustment.

Doing the same with savetxt:

In [153]: for i in range(4):
     ...:     np.savetxt('foo%s'%i,(np.column_stack((V,F[:,i]))))
     ...:     
In [154]: cat foo0
2.263318040000000053e+02 -3.764930340275589060e+03
2.288179570000000069e+02 -3.764933802256642139e+03
In [155]: cat foo1
2.263318040000000053e+02 -3.764930349409821247e+03
2.288179570000000069e+02 -3.764933812975268211e+03

This uses the default savetxt formatting. You could use what ever you prefer.

You could use T values to create the file names instead. foo%s%T[0,i]`?

答案 1 :(得分:0)

对于效果更好的矩阵,您可以使用 pprint ,例如以下代码:

from pprint import pprint

pprint('your-matrix')

答案 2 :(得分:0)

基于@hpaulj的回答:

F =  np.array([[-3764.9303402755891, -3764.9303494098212, -3764.9304257856452, -3764.9306329129422], [-3764.9338022566421, -3764.9338129752682, -3764.9338970928361, -3764.9341184863633]])    
V =  np.array([ 226.331804,  228.817957])
T =  np.array([[ 10.,   30.1,  50.2,  70.3], [ 10.,   30.1,  50.2,  70.3]])

for i in range(len(V)):
    np.savetxt('foo%s'%i,(np.column_stack((V, F[:,i]))))

我认为这也可以通过以下循环实现:

rows = F_all.shape[0]
cols = F_all.shape[1]

cols_T = T.shape[1]
rows_T = T.shape[0]

for indx, t  in zip(range(0, cols), range(0, cols_T) ):
   aux_T = T[:,t]
   aux_F = F_all[:,indx]

   output_array = np.vstack((VOLUME_EACH, aux_F)).T
   np.savetxt('F_vs_V_%0.2fK.dat'  %aux_T[0], output_array, header="Volume           F at %0.2fK" %aux_T[0], fmt="%0.13f")

如此处所示,生成的文件是相同的:

cat foo0

2.263318040000000053e+02 -3.764930340275589060e+03
2.288179570000000069e+02 -3.764933802256642139e+03

cat foo1

2.263318040000000053e+02 -3.764930349409821247e+03
2.288179570000000069e+02 -3.764933812975268211e+03

cat F_vs_V_10.00K.dat

# Volume           F at 10.00K
226.3318040000000 -3764.9303402755891
228.8179570000000 -3764.9338022566421

cat F_vs_V_30.10K.dat

# Volume           F at 30.10K
226.3318040000000 -3764.9303494098212
228.8179570000000 -3764.9338129752682