添加nd-array的选定列

时间:2016-06-27 22:11:53

标签: python numpy

我有一个3d数组,当以给定的方式打印时,

for i in range(5):
    print("======="+str(i)+"=========")
    for k in range(5):
        dos2[i][k]=atom[i][k][:5]
        print(dos2[i][k][:])

看起来像:

=======0=========
0[-4.052, 0.0, 0.0, 0.0, 0.0]
1[-3.995, 0.0, 0.0, 0.0, 0.0]
2[-3.938, -7.918e-34, -2.143e-34, -3.146e-35, -8.847e-36]
3[-3.88, -7.654e-30, -2.276e-30, -3.097e-31, -9.563e-32]
4[-3.823, -3.763e-26, -1.227e-26, -1.541e-27, -5.217e-28]
=======1=========
0[-4.052, 0.0, 0.0, 0.0, 0.0]
1[-3.995, 0.0, 0.0, 0.0, 0.0]
2[-3.938, -1.856e-33, -5.119e-34, -3.842e-35, -1.066e-35]
3[-3.88, -1.795e-29, -5.435e-30, -3.744e-31, -1.141e-31]
4[-3.823, -8.826e-26, -2.931e-26, -1.852e-27, -6.185e-28]
=======2=========
0[-4.052, 0.0, 0.0, 0.0, 0.0]
1[-3.995, 0.0, 0.0, 0.0, 0.0]
2[-3.938, -4.011e-34, -1.081e-34, -1.304e-35, -3.678e-36]
3[-3.88, -3.829e-30, -1.133e-30, -1.274e-31, -3.945e-32]
4[-3.823, -1.867e-26, -6.063e-27, -6.317e-28, -2.144e-28]
=======3=========
0[-4.052, 0.0, 0.0, 0.0, 0.0]
1[-3.995, 0.0, 0.0, 0.0, 0.0]
2[-3.938, -1.204e-32, -3.362e-33, -4.686e-34, -1.34e-34]
3[-3.88, -1.198e-28, -3.676e-29, -4.691e-30, -1.474e-30]
4[-3.823, -6e-25, -2.018e-25, -2.358e-26, -8.124e-27]
=======4=========
0[-4.052, 0.0, 0.0, 0.0, 0.0]
1[-3.995, 0.0, 0.0, 0.0, 0.0]
2[-3.938, -2.455e-33, -6.644e-34, -5.033e-35, -1.412e-35]
3[-3.88, -2.404e-29, -7.148e-30, -5.091e-31, -1.568e-31]
4[-3.823, -1.192e-25, -3.888e-26, -2.575e-27, -8.693e-28]

顶级循环i表示原子,k是二进制文件大小,二进制文件的值是[i,k,0],并且[i,k,[1:]]表示为{{ 1}}。

我想要得到的是每个箱子中所有原子的p之和,即比如说,

p

等等。

我怎么能得到它?

1 个答案:

答案 0 :(得分:1)

这是一个猜测,但我认为你想要总结axis=0

我喜欢混合各种形状,因此我们更清楚:

In [558]: atom=np.arange(3*4*5).reshape(3,4,5)

In [559]: atom
Out[559]: 
array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19]],

       [[20, 21, 22, 23, 24],
        [25, 26, 27, 28, 29],
        [30, 31, 32, 33, 34],
        [35, 36, 37, 38, 39]],

       [[40, 41, 42, 43, 44],
        [45, 46, 47, 48, 49],
        [50, 51, 52, 53, 54],
        [55, 56, 57, 58, 59]]])

In [560]: np.sum(atom, axis=0)
Out[560]: 
array([[ 60,  63,  66,  69,  72],
       [ 75,  78,  81,  84,  87],
       [ 90,  93,  96,  99, 102],
       [105, 108, 111, 114, 117]])

我从一个(3,4,5)数组开始,得到一个(4,5),将第一个(你的术语中的原子')压缩为一个值。

或者留下3d,单身第一维:

In [561]: np.sum(atom, axis=0, keepdims=True).shape
Out[561]: (1, 4, 5)

你想要切片的数组,如:

In [563]: np.sum(atom[:,1:,:], axis=0)
Out[563]: 
array([[ 75,  78,  81,  84,  87],
       [ 90,  93,  96,  99, 102],
       [105, 108, 111, 114, 117]])