繁体   English   中英

将字符串元素添加到列表中

[英]Add string elements to a list

我想将 for 循环中的字符串元素添加到列表中(对于每次迭代)并保持唯一的字符串值,

#Script will give an output in order to count occurence of BGC clusters per Bacteria Type

#!/usr/bin/env python
from Bio import SeqIO 
#Biopython package for input file (input and output assorted sequence file formats)
from Bio.SeqFeature import SeqFeature
import numpy as np
import sys
import pandas as pd

# Set the input file to be used and the output file to be written to
gbk_file = "E2N166_1.final.gbk" #example: "contig_1.final.gbk"
tsv_file = "cluster_count_file.txt"  #output file can be .csv, .txt etc...
cluster_output = open(tsv_file, "w")

#Extract the Cluster info from GBK file,
for seq_record in SeqIO.parse(gbk_file, "genbank"):
    cluster_list = []
    for seq_feat in seq_record.features:
        if seq_feat.type == "cluster":
            cluster_number = seq_feat.qualifiers["note"][0].replace(" ","_").replace(":","")
            cluster_type = seq_feat.qualifiers["product"][0]
            cluster_list.append(cluster_type)

            cluster_out.write("#" + cluster_number+ "\tCluster Type:" + cluster_type + "\n")

print("File Cluster Out")

output 看起来如下终端 Output

如果你想存储独特的元素,我建议你使用 set 而不是 list。 (你必须知道 set 不是有序的)

cluster_set = set()

但是如果你想坚持使用列表,你可以这样做:

if cluster_type not in cluster_list:
    cluster_list.append(cluster_type)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM