比较两个列表时如何在特定字符串后打印某些内容?

时间:2017-05-11 09:05:11

标签: python

我正在尝试比较列表中的字符串,如果字符串也在另一个列表中,我想在新行上打印一些内容。不幸的是,两个列表中的几个(数字可以变化)相同的变量在彼此之下,我想在最后一个下面打印TER。我哪里错了?

import sys
import argparse

def main(argv):
    parser = argparse.ArgumentParser(description='Add Ter records')
    parser.add_argument('infile', help='input file (PDB format)')
    parser.add_argument('outfile', help='output file (PDB format)')
    parser.add_argument('reference', help =' ref')
    args = parser.parse_args()

    resnum_1 =[]
    res_1 = []
    with open(args.infile, "r") as f, open(args.outfile, "w+") as of, open(args.reference,"r") as rf:
        for line in f:
            of.write(line)
        for line in rf:
            if line[0:3]== "TER":
                resnum = line[22:27]
                resnum_1.append(resnum)
                resnum_2 = []
                for i in resnum_1:
                    resnum_2.append(i.strip())
        of.seek(0)
        for line in of:
            if line [0:4]== "ATOM":
                res = line[22:27]
                res_1.append(res)
                res_2 = []
                for i in res_1:
                    res_2.append(i.strip())
                for x in res_2:
                        if x in resnum_2 and (res_2.index(x))+1 != x:
                            of.write("\nTER\n")
                        else:
                            continue

if __name__ == "__main__":
    main(sys.argv)

infile示例:

ATOM      1  N   GLU D 384      51.765  39.857  23.514  1.00  0.00           N  
ATOM      2  H1  GLU D 384      50.823  39.839  23.150  1.00  0.00           H  
ATOM      3  H2  GLU D 384      51.956  39.044  24.081  1.00  0.00           H  
ATOM      4  H3  GLU D 384      52.469  39.840  22.790  1.00  0.00           H  
ATOM      5  CA  GLU D 384      51.934  41.135  24.345  1.00  0.00           C  
ATOM      6  HA  GLU D 384      53.002  41.062  24.550  1.00  0.00           H  
ATOM      7  CB  GLU D 384      51.712  42.439  23.503  1.00  0.00           C  
ATOM      8  HB2 GLU D 384      52.307  42.297  22.600  1.00  0.00           H  
ATOM      9  HB3 GLU D 384      50.640  42.356  23.323  1.00  0.00           H  
ATOM     10  CG  GLU D 384      52.024  43.786  24.125  1.00  0.00           C  
ATOM     11  HG2 GLU D 384      52.138  44.557  23.363  1.00  0.00           H  
ATOM     12  HG3 GLU D 384      51.201  44.086  24.773  1.00  0.00           H  
ATOM     13  CD  GLU D 384      53.381  43.828  24.935  1.00  0.00           C  
ATOM     14  OE1 GLU D 384      53.634  43.069  25.869  1.00  0.00           O  

reference示例:

ATOM      1  N   GLU D 384      51.765  39.857  23.514  1.00  0.00           N  
ATOM      2  H1  GLU D 384      50.823  39.839  23.150  1.00  0.00           H  
ATOM      3  H2  GLU D 384      51.956  39.044  24.081  1.00  0.00           H  
ATOM      4  H3  GLU D 384      52.469  39.840  22.790  1.00  0.00           H  
ATOM      5  CA  GLU D 384      51.934  41.135  24.345  1.00  0.00           C  
ATOM      6  HA  GLU D 384      53.002  41.062  24.550  1.00  0.00           H  
ATOM      7  CB  GLU D 384      51.712  42.439  23.503  1.00  0.00           C  
ATOM      8  HB2 GLU D 384      52.307  42.297  22.600  1.00  0.00           H  
ATOM      9  HB3 GLU D 384      50.640  42.356  23.323  1.00  0.00           H  
ATOM     10  CG  GLU D 384      52.024  43.786  24.125  1.00  0.00           C  
ATOM     11  HG2 GLU D 384      52.138  44.557  23.363  1.00  0.00           H  
TER

ATOM     12  HG3 GLU D 384      51.201  44.086  24.773  1.00  0.00           H  
ATOM     13  CD  GLU D 384      53.381  43.828  24.935  1.00  0.00           C 

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,以下代码段可以为您提供帮助。

a = [1, 2, 3, 4, 7]
b = [1, 2, 5, 7, 16]
streak = False
for e in a:
    if e in b:
        streak = True
    elif streak:
        streak = False
        of.write("TER\n")
    of.write(e)
if e in b:
    of.write("TER\n")
  

1
  2
  TER
  3
  4
  7
  TER

它假设列表a的元素可以在列表b中的任何位置。 还要注意,它不标记序列的开头......只标记它们的结束。