我有一段代码生成一个包含分子的x,y和z坐标的文件。为了使它成为一个所谓的" xyz文件" (file.xyz),我需要在这个坐标文件中添加两行:1)顶行只包含一个数字,原子数; 2)注释行可以留空(或说"注释"或其他)。我的名为xyz
的变量看起来像这样
print(xyz)
给出
[['C\t' '-3.171284' '-0.739715' '0.525945']
['N\t' '-1.936557' '-0.026021' '0.202474']
['C\t' '-3.318626' '-1.011529' '2.043242']
['H\t' '-3.141982' '-1.708867' '0.014441']
['C\t' '-0.741793' '-0.677286' '0.129631']
['C\t' '0.462397' '0.162645' '-0.307099']
['N\t' '1.631324' '-0.381016' '0.368999']
['C\t' '0.559996' '0.147611' '-1.856616']
['H\t' '0.300250' '1.202007' '0.017165']
['H\t' '2.526359' '-0.068732' '0.017635']
['H\t' '1.567511' '-1.380033' '0.529925']
['O\t' '-0.635852' '-1.906598' '0.361505']
['H\t' '-2.000386' '0.966116' '0.025368']
['C\t' '-4.387139' '0.003940' '0.016667']
['O\t' '-4.106128' '1.302132' '-0.374180']
['O\t' '-5.528310' '-0.455442' '-0.014098']
['H\t' '-4.923659' '1.755530' '-0.673289']
['H\t' '0.662313' '-0.882934' '-2.213554']
['H\t' '1.440364' '0.716422' '-2.178038']
['H\t' '-0.324051' '0.596253' '-2.326387']
['H\t' '-2.470166' '-1.616700' '2.369974']
['H\t' '-4.246949' '-1.555151' '2.240849']
['H\t' '-3.326402' '-0.071610' '2.604601']]
原子数由len(xyz)
给出。我理想的输出应该是
print(xyz)
[[23 '' '' '']
['Comment' '' '' '']
['C\t' '-3.171284' '-0.739715' '0.525945']
['N\t' '-1.936557' '-0.026021' '0.202474']
['C\t' '-3.318626' '-1.011529' '2.043242']
['H\t' '-3.141982' '-1.708867' '0.014441']
['C\t' '-0.741793' '-0.677286' '0.129631']
['C\t' '0.462397' '0.162645' '-0.307099']
['N\t' '1.631324' '-0.381016' '0.368999']
['C\t' '0.559996' '0.147611' '-1.856616']
['H\t' '0.300250' '1.202007' '0.017165']
['H\t' '2.526359' '-0.068732' '0.017635']
['H\t' '1.567511' '-1.380033' '0.529925']
['O\t' '-0.635852' '-1.906598' '0.361505']
['H\t' '-2.000386' '0.966116' '0.025368']
['C\t' '-4.387139' '0.003940' '0.016667']
['O\t' '-4.106128' '1.302132' '-0.374180']
['O\t' '-5.528310' '-0.455442' '-0.014098']
['H\t' '-4.923659' '1.755530' '-0.673289']
['H\t' '0.662313' '-0.882934' '-2.213554']
['H\t' '1.440364' '0.716422' '-2.178038']
['H\t' '-0.324051' '0.596253' '-2.326387']
['H\t' '-2.470166' '-1.616700' '2.369974']
['H\t' '-4.246949' '-1.555151' '2.240849']
['H\t' '-3.326402' '-0.071610' '2.604601']]
我的解决方案(感觉有点笨拙):
nbofatoms = len(xyz)
xyz = np.insert(xyz, 0, '', axis=0)
xyz = np.insert(xyz, 0, '', axis=0)
xyz[0][0] = nbofatoms
xyz[1][0] = 'Comment'
那么,有更好的方法吗?
答案 0 :(得分:-1)
您可以内联压缩代码:
xyz = np.insert(xyz, 0, str(nbofatoms), axis=0)
xyz = np.insert(xyz, 0, 'comment', axis=0)