如何在python中插入计数器

时间:2013-09-25 09:55:26

标签: python math numpy

outfile2 = open('cordrandm.pdb', 'w')
for kk in text1:
if kk != '':
    outfile2.write(kk)
    outfile2.write('\n')

for alcord in alistcoord:
    uacoord = alcord[1]
    for blcord in blistcoord:
        ubcoord = blcord[1]    
        for arang in range(1000):
            arang = arang + 1
            x,y,z = uacoord
            x1,y1,z1 = (uniform(x-3.5,x+3.5), uniform(y-3.5,y+3.5), uniform(z-3.5,z+3.5))
            pacord = [x1,y1,z1]

            dist_pap = euDist(uacoord, pacord)
            if  3.5 > dist_pap > 2.5: 
               dist_pap1 = dist_pap

    outfile2.write('ATOM         OW  HOH X          %6.3f  %6.3f  %6.3f\n' % (pacord[0],  pacord[1],  pacord[2]))

outfile2.close()

从这段代码我得到像这样的输出文件

1------10 11-----20 21------30-----so on
123456789012345678901234567890123456789012345678901234
ATOM         OW  HOH X          10.533  22.567  61.777
ATOM         OW  HOH X           5.655  23.758  61.221
ATOM         OW  HOH X          10.125  20.476  68.503
ATOM         OW  HOH X          11.222  16.810  69.267
ATOM         OW  HOH X           4.351  18.904  66.256
ATOM         OW  HOH X           9.194  22.065  67.714
ATOM         OW  HOH X           4.347  16.527  68.278

我想为每一行'HOH'插入计数器并输出如下

ATOM         OW  HOH X   1      10.533  22.567  61.777
ATOM         OW  HOH X   2       5.655  23.758  61.221
ATOM         OW  HOH X  10      10.125  20.476  68.503
ATOM         OW  HOH X 100      11.222  16.810  69.267
ATOM         OW  HOH X 101       4.351  18.904  66.256
ATOM         OW  HOH X 102       9.194  22.065  67.714
ATOM         OW  HOH X1000       4.347  16.527  68.278

表示列范围23-26指定HOH的序列号。 我如何在特定列中插入HOH的计数,这不会改变我的输出格式。

提前致谢

2 个答案:

答案 0 :(得分:1)

就像你关于pacord的最后一个问题一样。只需将行改为:

outfile2.write('ATOM OW HOH X%4s %6.3f %6.3f %6.3f\n' % (hoh_serial, pacord[0], pacord[1], pacord[2]))

(考虑hoh_serial包含该行的序列号)

修改

如果你的意思是该行的序列号,那么你还应该添加:

from itertools import count
...
hoh_serial = count(1)
...
...
outfile2.write('ATOM         OW  HOH X%4s      %6.3f  %6.3f  %6.3f\n' % (hoh_serial.next(), pacord[0],  pacord[1],  pacord[2]))

答案 1 :(得分:0)

我认为你的意思是

for counter, blcord in enumerate(blistcoord):
    ...
    outfile2.write('ATOM ...', (counter,...) )

然后计数器就是你的柜台。