我正在尝试优化我的代码,因为当我尝试加载庞大的字典时,它变得非常慢。我认为这是因为它在字典中搜索一个键。我一直在阅读有关 python的内容defaultdict
,我认为这可能是一个很好的改进,但我没有在这里实现它。如您所见,这是一个分层字典结构。任何提示将不胜感激。
class Species:
'''This structure contains all the information needed for all genes.
One specie have several genes, one gene several proteins'''
def __init__(self, name):
self.name = name #name of the GENE
self.genes = {}
def addProtein(self, gene, protname, len):
#Converting a line from the input file into a protein and/or an exon
if gene in self.genes:
#Gene in the structure
self.genes[gene].proteins[protname] = Protein(protname, len)
self.genes[gene].updateProts()
else:
self.genes[gene] = Gene(gene)
self.updateNgenes()
self.genes[gene].proteins[protname] = Protein(protname, len)
self.genes[gene].updateProts()
def updateNgenes(self):
#Updating the number of genes
self.ngenes = len(self.genes.keys())
基因和蛋白质的定义是:
class Protein:
#The class protein contains information about the length of the protein and a list with it's exons (with it's own attributes)
def __init__(self, name, len):
self.name = name
self.len = len
class Gene:
#The class gene contains information about the gene and a dict with it's proteins (with it's own attributes)
def __init__(self, name):
self.name = name
self.proteins = {}
self.updateProts()
def updateProts(self):
#Update number of proteins
self.nproteins = len(self.proteins)