如何在python中打印重复单词的不同字符?

2024-06-08 13:33:17 发布

您现在位置:Python中文网/ 问答频道 /正文

文本文件PDBs.txt中的数据如下所示:

150L_A

150L_B

150L_C

150L_D

16GS_A

16GS_B

17GS_A

17GS_B

所需的最终结果是:

"First chain of 150L is A and second is B and third is C and forth is D"

"First chain of 16GS is A and second is B"

etc.

chains.txt输出文件中

谢谢你的帮助


Tags: and文件of数据txtchainisetc
2条回答

您只需通过拆分操作和循环即可实现这一点。 首先用空字符分割数据,以得到一个列表。然后,每个块由一个键和一个值组成,用下划线分隔。您可以迭代所有块,并将它们拆分为键和值。然后简单地创建一个python字典,其中包含每个键的所有值的数组

data = "150L_A 150L_B 150L_C 150L_D 16GS_A 16GS_B 17GS_A 17GS_B 18GS_A 18GS_B 19GS_A 19GS_B"

chunks = data.split()
result = {}

for chunk in chunks:
  (key, value) = chunk.split('_')
  if not key in result:
    result[key] = []
  result[key].append(value)

print(result)
# {'150L': ['A', 'B', 'C', 'D'], '16GS': ['A', 'B'], '17GS': ['A', 'B'], '18GS': ['A', 'B'], '19GS': ['A', 'B']}

您可以通过首先读取文件并将PDB和链标签提取到将PDB ID映射到链标签列表(这里称为^{)的字典来实现这一点。然后,您可以通过迭代这些结果并构造您指定的输出行,逐行写入“chains.txt”文件:

from collections import defaultdict                                             

results = defaultdict(list)                                                     
with open("PDBs.txt") as fh:                                                    
    for line in fh:                                                             
        line = line.strip()                                                     
        if line:                                                                
            pdb, chain = line.split("_")                                        
            results[pdb].append(chain)                                          

# Note that you would need to extend this if more than 4 chains are possible                                                
prefix = {2: "second", 3: "third", 4: "fourth"}                                 

with open("chains.txt", "w") as fh:                                             
    for pdb, chains in results.items():                                         
        fh.write(f"First chain of {pdb} is {chains[0]}")                        
        for ii, chain in enumerate(chains[1:], start=1):                        
            fh.write(f" and {prefix[ii + 1]} is {chain}")                       
        fh.write("\n")

“chains.txt”的内容:

First chain of 150L is A and second is B and third is C and fourth is D         
First chain of 16GS is A and second is B                                        
First chain of 17GS is A and second is B                                        
First chain of 18GS is A and second is B                                        
First chain of 19GS is A and second is B

相关问题 更多 >